简体   繁体   中英

Bars position in ggplot2

I'm making a graph using ggplot and I am not able to reduce the spaces between the bars. I can only do this by increasing the size of the bars, but I would like them to become thin and not so far away.

I've used the position_dodge(width = 0.5) but did not help me.

My Script

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)

Perhaps you're question is better answered by changing the output window size, not tweaking code in your ggplot function:

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)

You can do similar things if you are outputting it as a pdf or 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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