jeake
jeake

Reputation: 103

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:

enter image description 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.

Upvotes: 0

Views: 2890

Answers (1)

kluu
kluu

Reputation: 2995

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)) 

Upvotes: 4

Related Questions