I have the small data frame and I would like to build a bar chart with lines that show average levels across groups.
dataframe:
cpcost = c("Coll.Punishment","Coll.Punishment", "Ind.Punishment",
"Ind.Punishment")
color = c(0,1,0,1)
mysum = c(0.3408240, 0.2935982, 0.3460490, 0.1267057)
genlevel = c(0.3111111,0.3111111, 0.2181818, 0.2181818)
temp = as.data.frame(cbind(cpcost,color,mysum,genlevel))
So the result looks like this:
The problem is that now I am doing in in a really UGLY way (see below). I basically add horizontal error bars first. but then since there is a small space between errors bars for each bar:
I also draw a horizontal line to fill this small space between error bars. (If I draw the only horizontal bar it will start from the middle of bar).
I do believe that there is much more elegant way to reach the same result. How should I do it?
Thanks!
ggplot(temp, aes(group=cpcost),
y = genlevel ) +
geom_bar(stat = "identity",aes(x = factor(color),
y = mysum,
group=cpcost,
fill = factor(color))) +
geom_errorbar(aes(x=factor(color),
y=genlevel,
ymin=genlevel,
ymax=genlevel,
group=cpcost
)) +
geom_line( aes(x=factor(color), y=genlevel,group=cpcost)) +
labs(x = "Round", y = "Share of Emptying") +
scale_fill_discrete(name='Crime',
labels=c("No", "Yes" )) +
ggtitle('Whistleblowing')+
facet_grid(~cpcost)