条形图位于ggplot2中

时间:2013-10-07 15:42:05

标签: r graphics ggplot2

我正在使用ggplot创建图表,但我无法减少条形图之间的间距。我只能通过增加条形尺寸来做到这一点,但我希望它们变得薄而且不会那么遥远。

我使用了position_dodge(width = 0.5),但没有帮助我。

我的剧本

library(scales)
seguro <- matrix(0,4,3)
seguro <- as.data.frame(seguro)
seguro[,1] <- c("2010","2011","2012","2013")
seguro[,2] <-c(89,86,87,88)
seguro[,3] <-c("89%","86%","87%","88%")
names(seguro)[c(1)]<-c("Ano")
ggplot(seguro, aes(Ano, V2, fill=Ano))+
         geom_bar(width=0.3,stat="identity",
                 position="identity", aes(fill=Ano)) +
         scale_y_continuous(labels=percent) +
         geom_text(data=seguro,aes(x=Ano,label=V3),vjust=0)

1 个答案:

答案 0 :(得分:1)

通过更改输出窗口大小而不是调整ggplot函数中的代码,可能会更好地回答您的问题:

library(scales)
seguro <- matrix(0,4,3)
seguro <- as.data.frame(seguro)
seguro[,1] <- c("2010","2011","2012","2013")
seguro[,2] <-c(89,86,87,88)
seguro[,3] <-c("89%","86%","87%","88%")
names(seguro)[c(1)]<-c("Ano")

x11(height=7,width=5)

ggplot(seguro, aes(Ano, V2, fill=Ano))+
         geom_bar(width=0.3,stat="identity",
                 position="identity", aes(fill=Ano)) +
         scale_y_continuous(labels=percent) +
         geom_text(data=seguro,aes(x=Ano,label=V3),vjust=0)

如果您将其输出为pdf或jpeg,则可以执行类似的操作:

pdf("test.pdf",height=7,width=5)
ggplot(seguro, aes(Ano, V2, fill=Ano))+
         geom_bar(width=0.3,stat="identity",
                 position="identity", aes(fill=Ano)) +
         scale_y_continuous(labels=percent) +
         geom_text(data=seguro,aes(x=Ano,label=V3),vjust=0)
dev.off()