ggplot2 shifting bars to only overlap in groups

490 views
Skip to first unread message

Hörmetjan Yiltiz

unread,
Feb 12, 2015, 9:24:49 AM2/12/15
to ggp...@googlegroups.com
Hi all,

I have four factors for a continuous time variable along with its confidence interval. I would like to produce a publication quality error bar chart that is clear to understand. For now, I used colors, x axis position, facets and alpha level to distinguish them. 

I would like to overlap each pairs of bars with the same color a bit as a group, but not overlap each and every bars with each other.

Here is a minimal example:

N = 32
df<- data.frame(gender=gl(2,1,N, c("male","female")),
          direction=gl(2,2,N, c("up","down")),
          condition=gl(4,4,N, c("c1","c2","c3","c4")),
          location=gl(2,16,N, c("east","west")),
          t=rnorm(N, 1, 0.5),
          ci=abs(rnorm(N, 0, 0.2)))
pp <- 
  ggplot(df, aes(x=gender, y=t, fill=condition, alpha=direction)) + 
  facet_grid(location~.) +
  geom_bar(position=position_dodge(.9), stat="identity", color="black") +
  geom_errorbar(aes(ymin=t-ci, ymax=t+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9)) +
  scale_alpha_discrete(range= c(0.4, 1))
pp



In the attachment, I have added the output figure, while manually editing the SVG file to make the lower-left group of bars to make them as I wanted. (The spacing in between each pair is not necessarily required.)

Rajesh Sahasrabuddhe

unread,
Feb 14, 2015, 7:26:07 AM2/14/15
to ggp...@googlegroups.com
I could be wrong - but I don't think you can achieve what you are after with geom_bar. It's possible you could do it with geom_rect.

Hörmetjan Yiltiz

unread,
Feb 15, 2015, 1:32:32 PM2/15/15
to ggp...@googlegroups.com
I think maybe it is possible to first produce a blank axis, and then splitting the data frame by the value of direction. Then add the goem_bar and goem_errorbar for the blank axis for the first split, then add them for the second half split. This is actually a slit-apply-combine strategy. It would be perfect if we could come up with the way to do that using d_ply().

ByungJu Kim

unread,
Feb 16, 2015, 4:08:42 PM2/16/15
to ggp...@googlegroups.com
Hello, How about this one ?

df$Xaxis<-paste(df$gender,df$condition,sep="_")

pp <- 
  ggplot(df, aes(x=Xaxis, y=t, fill=condition, alpha=direction)) + 
  facet_grid(location~.) +
  geom_bar(position=position_dodge(.9), stat="identity", color="black") +
  geom_errorbar(aes(ymin=t-ci, ymax=t+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9)) +
  scale_alpha_discrete(range= c(0.4, 1))
pp

I think that you can hide the X-axis text and write new one.

Sincerely

BJ Kim.

ByungJu Kim

unread,
Feb 16, 2015, 4:20:23 PM2/16/15
to ggp...@googlegroups.com
or  you can use

pp <- 
  ggplot(df, aes(x=condition, y=t, fill=condition, alpha=direction)) + 
  facet_grid(location ~ gender) +
  geom_bar(position=position_dodge(.9), stat="identity", color="black") +
  geom_errorbar(aes(ymin=t-ci, ymax=t+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9)) +
  scale_alpha_discrete(range= c(0.4, 1))
pp

I think that the problem is that X value is not "condition" so the bars doesn't  appeared as you wished.

Hope it works :)

ByungJu Kim

Hörmetjan Yiltiz

unread,
Feb 17, 2015, 8:58:39 AM2/17/15
to ggp...@googlegroups.com
This is almost exactly what I was asking for. So what I should have done was to explicitly set the x axis, then split the plot using other factors on the grid.

Thanks so much!
Reply all
Reply to author
Forward
0 new messages