Error bars width in ggplot2

I have some data with associated standard errors and would like to display them with errors. This is what I have:

# generate some data hod <- data.frame(h = c(1:24,1:24,1:24), mean = 1:(24*3) + runif(24*3, 0, 5),ci = runif(24*3, 0, 2), t = c(rep("a",24),rep("b",24),rep("c",24))) pd <- position_dodge(0.3) dayplot <- ggplot(hod, aes(x=h, y=mean, colour=as.factor(t),group=as.factor(t))) + geom_line(position=pd, size=1) + geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci), width=1, size=0.5, position=pd) + geom_point(position=pd, shape=21, size=1, fill="white") + scale_x_continuous(limits=c(-0.5,23.5), breaks=c(0:8*3), labels=ifelse( c(0:8*3) < 10, paste('0',c(0:8*3),':00',sep=''), paste(c(0:8*3),':00',sep='') ) ) + xlab("Hour of day") + ylab(ylabel) + labs(title = varlabels[var]) + theme_minimal() + theme(plot.margin = unit(c(1,0,1,1), "cm"), axis.title.x = element_text(vjust=-1), axis.title.y = element_text(angle=90, vjust=0), legend.margin = unit(c(0), "cm"), legend.key.height = unit(c(0.9), "cm"), panel.grid.major = element_line(colour=rgb(0.87,0.87,0.87)), panel.grid.minor = element_blank(), plot.background = element_rect(fill = rgb(0.97,0.97,0.97), linetype=0) ) 

Probably the only thing that might be interesting:

 geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci), width=1, size=0.5, position=pd) 

He gives: all

Now, when I group the data using a factor variable ( as.factor(t) ), I get several rows instead of the one that I want, BUT, as you can see, the horizontal lines on the errors are narrower and I can’t understand why. I tried to change and even remove the width and size geom_errorbar , but nothing happens. Is there a way to have the same horizontal line width for each chart, regardless of the data? I mean, why does this have to change? Or does this width convey some information?

enter image description here

+9
r charts ggplot2
source share
2 answers

Below is a reproducible example using random data. The fix for the problem is to multiply the width by the number of classes / factors that you have. In the graph below, since I used three factors using a width of 3, the problem is fixed. ggplot2 seems to calculate the relative width from the number of data points in your data set, rather than the numerical values ​​along the x axis. This is an (IMO) error.

 library(ggplot2) library(grid) #plot with factors hod <- data.frame(h = c(1:24,1:24,1:24), mean = 1:(24*3) + runif(24*3, 0, 5),ci = runif(24*3, 0, 2), t = c(rep("a",24),rep("b",24),rep("c",24))) pd <- position_dodge(0.3) dayplot <- ggplot(hod, aes(x=h, y=mean, colour=as.factor(t),group=as.factor(t))) + geom_line(position=pd, size=1) + geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci), width=1, size=0.5, position=pd) + geom_point(position=pd, shape=21, size=1, fill="white") + scale_x_continuous(limits=c(-0.5,23.5), breaks=c(0:8*3), labels=ifelse( c(0:8*3) < 10, paste('0',c(0:8*3),':00',sep=''), paste(c(0:8*3),':00',sep='') ) ) + xlab("Hour of day") + theme_minimal() + theme(plot.margin = unit(c(1,0,1,1), "cm"), axis.title.x = element_text(vjust=-1), axis.title.y = element_text(angle=90, vjust=0), legend.margin = unit(c(0), "cm"), legend.key.height = unit(c(0.9), "cm"), panel.grid.major = element_line(colour=rgb(0.87,0.87,0.87)), panel.grid.minor = element_blank(), plot.background = element_rect(fill = rgb(0.97,0.97,0.97), linetype=0) ) print(dayplot) #plot without factors hod <- data.frame(h = c(1:24,1:24,1:24), mean = 1:(24) + runif(24, 0, 5),ci = runif(24, 0, 2)) pd <- position_dodge(0.3) dayplot <- ggplot(hod, aes(x=h, y=mean)) + geom_line(position=pd, size=1) + geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci), width=1, size=0.5, position=pd) + geom_point(position=pd, shape=21, size=1, fill="white") + scale_x_continuous(limits=c(-0.5,23.5), breaks=c(0:8*3), labels=ifelse( c(0:8*3) < 10, paste('0',c(0:8*3),':00',sep=''), paste(c(0:8*3),':00',sep='') ) ) + xlab("Hour of day") + theme_minimal() + theme(plot.margin = unit(c(1,0,1,1), "cm"), axis.title.x = element_text(vjust=-1), axis.title.y = element_text(angle=90, vjust=0), legend.margin = unit(c(0), "cm"), legend.key.height = unit(c(0.9), "cm"), panel.grid.major = element_line(colour=rgb(0.87,0.87,0.87)), panel.grid.minor = element_blank(), plot.background = element_rect(fill = rgb(0.97,0.97,0.97), linetype=0) ) print(dayplot) 
+5
source share

I managed to solve a similar problem. In my case, I wanted to set the same sizes of both horizontal and vertical error heads, regardless of the aspect ratio of the graph.

Based on the original code posted:

 f <- ggplot_build(dayplot) f$plot$layers[[5]]$geom_params$width <- 0.02 * diff(f$layout$panel_params[[1]]$x.range) f$plot$layers[[6]]$geom_params$height <- 0.02 * diff(f$layout$panel_params[[1]]$y.range) dayplot <- f$plot 

This will set the error bar title to 2% of the axis range. Maybe it can solve your problem.

0
source share

All Articles