[R] ggplot2 shifting bars to only overlap in groups
Hörmetjan Yiltiz
hyiltiz at gmail.com
Thu Feb 12 15:08:36 CET 2015
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.)
Best
,
========================
He who is worthy to receive his days and nights is worthy to receive* all
else* from you (and me).
The Prophet, Gibran Kahlil
More information about the R-help
mailing list