met*_*zoa 3 overlay r ggplot2 errorbar
我试图将两个条形图叠加在彼此之上,而不是旁边。数据来自同一个数据集。我想要 x 轴上的“块”和“开始”和“结束”作为覆盖条形图。
Block Start End
1 P1L 76.80 0.0
2 P1S 68.87 4.4
3 P2L 74.00 0.0
4 P2S 74.28 3.9
5 P3L 82.22 7.7
6 P3S 80.82 17.9
Run Code Online (Sandbox Code Playgroud)
我的脚本是
ggplot(data=NULL,aes(x=Block))+
geom_bar(data=my_data$Start,stat="identity",position ="identity",alpha=.3,fill='lightblue',color='lightblue4')+
geom_bar(data=my_data$End,stat="identity",position ="identity",alpha=.8,fill='pink',color='red')
Run Code Online (Sandbox Code Playgroud)
我得到错误:ggplot2 不知道如何处理数字类的数据
我也试过
ggplot(my_data,aes(x=Block,y=Start))+
geom_bar(data=my_data$End, stat="identity",position="identity",...)
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎样才能做到这一点?谢谢你。
编辑:
如何获得闪避叠加条?
我编辑这篇文章,因为我的下一个问题是相关的,因为它与我原来的帖子相反。
@P.merkle
我不得不将我的绘图更改为四个条形图,显示所有标记为 L 和 S 的区块的平均值。 L 代表沿海地区,S 代表沿海地区。他们进行了两种处理:正常和减少。
我已经计算了平均值和它们的标准差。我需要四个带有各自误差条的条形: Normal/Littoral , Reduced/Littoral , Normal/Sublittoral , Reduced/Sublittoral 。
问题是当我绘制它时,沿海酒吧和两个亚沿海酒吧相互重叠!所以现在我希望它们不要重叠!我怎样才能让它发生?我已经尝试了各种position = 'dodge'和position = position_dodge(newdata$Force),但没有运气......
我的newdata包含以下信息:
Zonation Force N mean sd se
1 Litoral Normal 6 0.000000 0.000000 0.000000
2 Litoral Redusert 6 5.873333 3.562868 1.454535
3 Sublitoral Normal 6 7.280000 2.898903 1.183472
4 Sublitoral Redusert 6 21.461667 4.153535 1.695674
我的脚本是这样的:
ggplot(data=cdata,aes(x=newdata$Force,y=newdata$mean))+
geom_bar(stat="identity",position ="dodge",
alpha=.4,fill='red', color='lightblue4',width = .6)+
geom_errorbar(aes(ymin=newdata$mean-sd,ymax=newdata$mean+sd),
width=.2, position=position_dodge(.9))
Run Code Online (Sandbox Code Playgroud)
结果很不幸是这样
至于误差线,那里显然有四个条,但它们重叠。请问,我该如何解决这个问题?
如果您不需要图例,解决方案 1可能适合您。它更简单,因为它以宽格式保存您的数据。
如果您需要图例,请考虑解决方案 2。它要求您的数据从宽格式转换为长格式。
您可以在单个几何体的级别上完善您的美学规范(此处为geom_bar):
ggplot(data=my_data, aes(x=Block)) +
geom_bar(aes(y=Start), stat="identity", position ="identity", alpha=.3, fill='lightblue', color='lightblue4') +
geom_bar(aes(y=End), stat="identity", position="identity", alpha=.8, fill='pink', color='red')
Run Code Online (Sandbox Code Playgroud)
要添加图例,首先使用reshape2::melt将数据框从宽格式转换为长格式。这给你两列,
variable列(“开始”与“结束”),value列现在使用该variable列来定义您的图例:
library(reshape2)
my_data_long <- melt(my_data, id.vars = c("Block"))
ggplot(data=my_data_long, aes(x=Block, y=value, fill=variable, color=variable, alpha=variable)) +
geom_bar(stat="identity", position ="identity") +
scale_colour_manual(values=c("lightblue4", "red")) +
scale_fill_manual(values=c("lightblue", "pink")) +
scale_alpha_manual(values=c(.3, .8))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14006 次 |
| 最近记录: |