Reputation: 5
Been scouring the site (and others) for a solution to the problem pictured below. I can't get the error bars to move from their 'low' position, despite trying lots of different solutions to similar problems I have encountered here and elsewhere.
The problem occurs whether I produce one graph alone or the two and combine them with gridExtra
.
Here is the code and a sample of each dataset included:
First dataset example:
Group PSQI Time_Point
1 Control 2 Baseline
2 Control 2 Baseline
3 Control 2 Baseline
4 Control 13 Baseline
5 Control 1 Baseline
6 Control 7 Baseline
Second:
Group ESS Time_Point
1 Control 3 Baseline
2 Control 4 Baseline
3 Control 1 Baseline
4 Control 0 Baseline
5 Control 7 Baseline
6 Control 11 Baseline
Code:
library(gridExtra)
library(ggplot2)
library(grid)
p1 <- ggplot(PSQI_Graph,aes(fill=Group,y=PSQI,x=Time_Point)) +
geom_bar(position="dodge",stat="identity", width = 0.50) +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position =
position_dodge(.5), width = .25)+
guides(fill = F)+
scale_y_continuous(name = "Mean PSQI Score", limits=c(0,25))+
xlab("Time Point") +
guides(fill = guide_legend(keywidth = 1.5, keyheight = 1))+
theme_bw()
Would really like to get this data out, so would appreciate any suggestions.
Upvotes: 0
Views: 557
Reputation: 994
It would be better to use geom_errorbar
. First, you'll have to make a new data.frame
with one line per bar, including the standard error (I added an "After" timepoint to show how this looks):
Group PSQI Time_Point StdErr
1 Control 4.50 Baseline 1.91
2 Control 7.17 After 0.79
Then, this ggplot
call will work:
ggplot(psqi, aes(fill=Group, y=PSQI, x=Time_Point, ymin=PSQI-StdErr, ymax=PSQI+StdErr)) + #the ymin and ymax parameters here tell it where to put the top and bottom error bars
geom_bar(stat="identity") +
geom_errorbar() #you can change the width and thickness of the bars
Yielding this image:
Upvotes: 2