简体   繁体   中英

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?

I'm trying to create a horizontal bar chart that visualizes abundance at specified depth intervals at both day and night. Essentially for those who are familiar with diel vertical migration (dvm), I'm trying to visualize that.

Here's a link to an example of what I'm talking about, specifically the bottom part of the figure. And another one.

I've been able to get this so far

在此处输入图像描述

but I'd like to make the duplicates go side by side instead with the middle divider indicating the change in day vs. night. So instead of it going 5, 4, 3, 2, 1, 5, 4, 3, 2, 1 on the y-axis it will have one set of 5-1 on the left and another set on the right, with the columns being directly side-by-side. Having one side shaded and another white would be super useful also. I'm also wondering how to add the error bars for the standard deviation too.

Here's the code that I have so far:

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)

Here is some example data

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

Does anyone know how to do this? Any help would be great! Thank you!

I think this is what you're looking for:-)

The ggplot2 and dplyr libraries would be useful to you! You can use dplyr::filter to extract all the Fish data instead of manually doing so for each variable.

Then, you can use ggplot to make the graph. I plotted Day with correct Average values, and the Night data with negative Average values. Then, flipped the coordinates (so the graph is verticle). Then, relabeled the y-axis since you won't want to show negative values.

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

在此处输入图像描述

To add standard deviation bars, you can add the following two geometries.

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()

在此处输入图像描述

The standard deviations in the data are quite big, so it does look a little strange, unfortunately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM