简体   繁体   中英

Align bars and ticks with geom_bar

My question is related to this answer by stibu. I am using geom_bar and I am trying to center the bars between the x-axis ticks (see the second graph of the answer).

Here is an example:

grp<-letters[1:13]
count<-c(38,591,549,487,419,363,276,276,164,68,31,10,5)
data<-data.frame(grp,count)
ggplot(data,aes(x=grp,y=count))+geom_bar(stat="identity")

Which gives the following:

在此处输入图片说明

  • My x axis is made of character variables so I can not use the "+0,5" trick.
  • There is no boundary or center option for geom_bar as suggested here

So how do I align bars so that their left border is aligned with the tick?

I am using R version 3.4.0 and RStudio 1.0.143.

Is that what you want ?

require(ggplot2)

grp<-letters[1:13]
count<-c(38,591,549,487,419,363,276,276,164,68,31,10,5)
data<-data.frame(grp,count)
ggplot(data,aes(x=grp,y=count))+
  geom_bar(stat="identity",  
           width = 0.9, 
           position = position_nudge(x = 0.5)) 

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