繁体   English   中英

ggplot2中的条形位置

[英]Bars position in 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)

也许您的问题可以通过更改输出窗口大小而不是在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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM