如何在 R 中创建一个基于 x 轴上的附加变量在中间拆分的水平条形图?

How can I create a horizontal bar chart in R that is split in the middle based off an additional variable on the x-axis?

我正在尝试创建一个水平条形图,以显示白天和黑夜指定深度间隔的丰度。对于那些熟悉昼夜垂直迁移 (dvm) 的人来说,我试图将其形象化。

这是 link to an example of what I'm talking about, specifically the bottom part of the figure. And another 一个。

到目前为止我已经能够得到这个

但我想让副本并排放置,而不是用中间分隔线指示白天与夜晚的变化。因此,它不会在 y 轴上移动 5、4、3、2、1、5、4、3、2、1,而是在左侧有一组 5-1,在右侧有另一组,其中列直接并排。一侧有阴影,另一侧有白色也会非常有用!我也想知道如何为标准偏差添加误差线。

这是我目前的代码:

abundance<-c(BroadClassification$Average[BroadClassification$BroadClassification=="Fish"])
fish<-c(BroadClassification$BroadClassification[BroadClassification$BroadClassification=="Fish"])
depth<-c(BroadClassification$ï..Net[BroadClassification$BroadClassification=="Fish"])
stdev<-c(BroadClassification$StdDev[BroadClassification$BroadClassification=="Fish"])
cols<- c(BroadClassification$DayNight[BroadClassification$BroadClassification=="Fish"])
bp1 <- barplot(abundance, main="Preliminary Broad Classification", horiz=TRUE, names.arg=depth)

这是一些示例数据

1  Net,DayNight,BroadClassification,Average,StdDev
2             1,Day,Cephalopod,0.5,0.08
3             1,Day,Crustacea,2,3.2
4                  1,Day,Fish,1,4.0
5            2,Day,Cephalopod,0.6,0.1
6             2,Day,Crustacea,6,2.3
7                  2,Day,Fish,7,8.1
8            3,Day,Cephalopod,0.2,0.2
9             3,Day,Crustacea,8,2.1
10                   3,Day,Fish,9,7.1
11           4,Day,Cephalopod,1,1.0
12            4,Day,Crustacea,0.3,0.2
13                 4,Day,Fish,4,7.8
14            5,Day,Cephalopod,5.2,6.1
15            5,Day,Crustacea,0.4,0.3
16                 5,Day,Fish,1.6,2.1
17         1,Night,Cephalopod,0.1,0.2
18          1,Night,Crustacea,1.8,4.2
19               1,Night,Fish,1.4,0.7
20          2,Night,Cephalopod,0.2,0.1
21          2,Night,Crustacea,3.2,8.9
22               2,Night,Fish,14,11.1
23         3,Night,Cephalopod,0.1,0.4
24           3,Night,Crustacea,1.2,2.4
25               3,Night,Fish,7.8,10.1
26         4,Night,Cephalopod,0.3,0.8
27          4,Night,Crustacea,4.2,4.1
28               4,Night,Fish,3.1,3.1
29         5,Night,Cephalopod,0.4,0.2
30          5,Night,Crustacea,0.9,0.1
31               5,Night,Fish,1.2,1.2

有人知道怎么做吗?任何帮助都会很棒!谢谢!

我想这就是您要找的:-)

ggplot2dplyr 库会对您有用!您可以使用 dplyr::filter 提取所有 Fish 数据,而不是为每个变量手动提取数据。

然后,您可以使用ggplot来制作图表。我用正确的平均值绘制了白天,用负平均值绘制了夜间数据。然后,翻转坐标(因此图形是垂直的)。然后,重新标记 y 轴,因为您不想显示负值。

library(ggplot2)
library(dplyr)

BroadClassificationFish <- BroadClassification %>% filter(BroadClassification == "Fish")

plot <- ggplot(BroadClassificationFish, aes(x = Net)) +
  geom_col(data = subset(BroadClassificationFish, DayNight == "Day"), 
           aes(y = Average, fill = 'Day')) +
  geom_col(data = subset(BroadClassificationFish, DayNight == "Night"), 
           aes(y = -Average, fill = 'Night')) + coord_flip() +
  scale_y_continuous(breaks = seq(-20,20, by = 4),
                     labels = (c(seq(20, 0, by = -4), seq(4,20,by=4))))
  
plot

要添加标准偏差条,您可以添加以下两个几何图形。

geom_errorbar(data = subset(BroadClassificationFish, DayNight == "Day"),
                #aes(ymin = Average - StdDev, ymax = Average + StdDev)) +
geom_col(data = subset(BroadClassificationFish, DayNight == "Night"), 
           aes(y = -Average, fill = 'Night')) + coord_flip()

数据的标准偏差很大,所以看起来有点奇怪,不幸的是。