ggplot2中错误栏的宽度 [英] Width of error bars in ggplot2
问题描述
我有一些与标准错误关联的数据,并希望用错误栏显示这些数据。这就是我所拥有的:
pre $ #生成一些数据
hod< - data.frame(h = c(1 :24,1:24,1:24),平均值= 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,color = 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',
paste(c(0:8 * 3),':00',sep ='')
)
)+
xlab(Hour of day)+ ylab(ylabel)+ labs(title = varlabels [var])+
theme_minimal()+
theme .margin = unit(c(1,0,1,1),cm),
axis.title.x = element_text(vjust = -1),
axis.title.y = element_text (c(0),cm),
legend.key.height =单位(c(0.9),cm,单位为角度= 90,vjust = 0),
legend.margin = ),
panel.grid.major = element_line(color = 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)
)
只有感兴趣的东西可能是:
$ p $ geom_errorbar(aes(ymin = mean-ci,ymax = mean + ci),
width = 1,
size = 0.5,
position = pd)
它给出:
现在,当我通过因子变量( as.factor(t)
)对数据进行分组时,我得到了几条线,而不是一条,这是我想要的,但是,正如你所看到的,错误条的水平线更窄,我不知道为什么。我尝试更改,甚至取消宽度
和大小
属性 geom_errorbar
,但没有任何反应。无论数据如何,是否有办法让每张图表具有相同宽度的水平线?我的意思是,为什么它会有所不同?还是这个宽度传达了一些信息?
以下是使用随机数据的可重现示例。解决问题的方法是将宽度乘以您拥有的类/因素的数量。在下面的图中,由于我使用了三个因素,使用3的宽度解决了这个问题。 ggplot2似乎通过数据集中数据点的数量来计算相对宽度,而不是x轴上的数值。这是(IMO)的一个错误。
库(ggplot2)
库(网格)
#plot with factor $ b (24 * 3)+ runif(24 * 3,0,5),ci = 1(24 * 3) (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,color = 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,
(0:8 * 3),
标签= 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 = 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 factor
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),
widt h = 1,
size = 0.5,
position = pd)+
geom_point(position = pd,shape = 21,size = 1,fill =white)+
scale_x_continuous(限制= 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 ,
legend.margin =单位(c(0),cm),
legend.key.height =单位(c(0.9),cm),
panel.grid.major = element_line(color = rgb(0.87,0.87,0.87)),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = rgb( 0.77,0.97,0.97),linetype = 0)
)
print(dayplot)
I have some data with standard errors associated and would like to display these with error bars. That's 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)
)
The only thing of interest is probably:
geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci),
width=1,
size=0.5,
position=pd)
It gives:
Now when I group the data by a factor variable (as.factor(t)
), I get several lines instead of one, which is what I want, BUT, as you can see, the horizontal lines at the error bars are more narrow, and I can't figure out why. I tried changing and even taking away the width
and size
attribute of geom_errorbar
, but nothing happens. Is there a way to have the same width of the horizontal lines for every chart, no matter the data? I mean, why should it vary? Or does that width convey some information?
Below is a reproducible example using random data. The fix to the problem is to multiply the width by the number of classes/factors that you have. In the plot below, since I used three factors, using a width of 3 fixes the problem. ggplot2 seems to calculate the relative width by the number of data points in your dataset, rather than the numeric values on the x-axis. This is (IMO) a bug.
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)
这篇关于ggplot2中错误栏的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!