标准化条形图的宽度和ggplot

时间:2017-08-17 14:56:57

标签: r ggplot2 bar-chart

我很好奇你是否可以标准化条形图中条形的宽度,从而使用ggplot制作图形时的误差条。我的一些X因子变量(Hour.Bin)中没有数据,所以当我绘制图时,每个条的宽度和误差条都会被调整以反映当时缺少的数据。

这是我正在使用的数据帧的第一行和最后几行。我已经根据HOBOLocation变量(5个级别)创建了汇总统计数据,以便绘制均值和标准差的条形图。

    Hour.Bin Lake.Livewell N     Mean    Median   SD        SE     HOBOLocation
1     5:00      Livewell   2 31.62550 31.6255 0.5055813 0.35750000 Flow Through
2     6:00      Livewell  59 30.91512 31.2680 1.0607782 0.13810156 Flow Through
3     7:00      Livewell 213 29.35422 29.6520 2.1084091 0.14446581 Flow Through
4     8:00      Livewell 325 29.24637 29.4520 1.6922953 0.09387165 Flow Through
5     9:00      Livewell 360 29.35180 29.3520 1.5650980 0.08248791 Flow Through
6    10:00      Livewell 366 29.57598 29.5520 1.5749769 0.08232536 Flow Through

    Hour.Bin Lake.Livewell   N   Mean    Median   SD        SE     HOBOLocation
51    14:00 Lake - Pelagic 125 30.77400 30.862 1.272879 0.1138497 Lake - Pelagic
52    15:00 Lake - Release  92 31.57482 31.268 2.016206 0.2102040 Lake - Release
53    15:00 Lake - Pelagic  81 30.98407 30.963 1.567551 0.1741724 Lake - Pelagic
54    16:00 Lake - Release  15 32.45160 31.472 2.676622 0.6911008 Lake - Release
55    16:00 Lake - Pelagic  14 32.37864 32.813 1.662311 0.4442713 Lake - Pelagic
56     <NA> Lake - Release   1      NaN     NA      NaN       NaN Lake - Release

p <- ggplot(data = subset(HOBOSummaryEverything,!is.na(levels(Hour.Bin)[Hour.Bin])), aes(x = Hour.Bin, y = Mean, fill=HOBOLocation))+theme_bw()
p + geom_bar(stat = "identity", colour="gray50",position = position_dodge(0.75),width=0.55,alpha=0.7) +
geom_errorbar(limits, position = position_dodge(0.75), width = 0.30, colour="black", linetype = 1, size=1) +
ggtitle(expression(atop(bold("Mean Temps by HOBO Location"), atop(italic("All Tournaments"), "")))) +
scale_fill_manual(values=c("gray30","gray60","gray90","green","blue"), labels=c("Flow Through", "Water Recirculation", "Flow Through and Water  Recirculation", "Release","Pelagic"))+
scale_y_continuous(expression(bold("Temp"*~(degree*C))),limits=c(0.0,40.0),breaks=seq(0.0,40.0,by=5.0),labels=fmt(),expand=c(0.02,0.02))+
scale_x_discrete("Hour Bin",limits=c(levels(HOBOSummaryEverything$Hour.Bin)[1:12]))

制作以下图表:

enter image description here

如果我不能使条宽和误差条宽度相同,这并不是什么大不了的事,但我想我会看看是否有一个简单的解决方案。为了清楚我想要制作什么,我希望条形宽度与所有5个因素水平在小时区内都有数据的情况相同(在我的大部分时间都是如此),即使像5小时一样: 00只有一个因子级别的数据(流通)。

0 个答案:

没有答案