Stacked bars with 2 groups and error bars

huangapple go评论77阅读模式
英文:

Stacked bars with 2 groups and error bars

问题

You can create a stacked bar plot with two grouping variables using the ggplot2 library in R. Here's an example code based on your dataset:

library(ggplot2)

# Create a summary dataset
summdata <- summarySE(datas, measurevar = "Corg", groupvars = c("Time", "Treatment", "Fraction"), na.omit = TRUE)

# Create the stacked bar plot
plot <- ggplot(summdata, aes(x = Time, y = Corg, fill = Fraction)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = Corg - se, ymax = Corg + se),
                width = 0.2,
                position = position_dodge(.9)) +
  facet_wrap(~Treatment)  # Separate plots by Treatment

plot

This code will create a stacked bar plot with two grouping variables, where Time is on the x-axis, Corg is on the y-axis, and the bars are filled based on the Fraction variable. Error bars are also added.

Remember to install and load the necessary packages (ggplot2 and Rmisc) before running the code.

英文:

I'm trying to create a stacked bar plot with 2 groups and error bars from a soil data set. I've got the factors Time = c(May 21, July 21, July 22), Treatment = c(T1, T2, T3, T4), and Fraction = c(Fine earth, Plant remains). There is also 'Repl' and 'Parcel' but these are not relevant for plotting. I'd like to plot the dependent variable Corg with Time in the x axis and grouped by Treatment and Fraction, the latter stacked. Therefore, 'Fine earth' stacked onto 'Plant remains'.

Here's the output of dput():

dput(datas)
structure(list(Treatment = c(&quot;T1&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T2&quot;, &quot;T2&quot;, 
&quot;T2&quot;, &quot;T2&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T1&quot;, 
&quot;T1&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, 
&quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, 
&quot;T2&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T1&quot;, &quot;T1&quot;, 
&quot;T1&quot;, &quot;T1&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T4&quot;, 
&quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, 
&quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T4&quot;, &quot;T1&quot;, &quot;T1&quot;, &quot;T1&quot;, 
&quot;T1&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T3&quot;, &quot;T4&quot;, &quot;T4&quot;, 
&quot;T4&quot;, &quot;T4&quot;), Repl = c(&quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, 
&quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, 
&quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, 
&quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, 
&quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, 
&quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, 
&quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, 
&quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, 
&quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, &quot;R4&quot;, &quot;R1&quot;, &quot;R2&quot;, &quot;R3&quot;, 
&quot;R4&quot;), Parcel = structure(c(1L, 5L, 9L, 13L, 2L, 6L, 10L, 14L, 
3L, 7L, 11L, 15L, 4L, 8L, 12L, 16L, 1L, 5L, 9L, 13L, 2L, 6L, 
10L, 14L, 3L, 7L, 11L, 15L, 4L, 8L, 12L, 16L, 1L, 5L, 9L, 13L, 
2L, 6L, 10L, 14L, 3L, 7L, 11L, 15L, 4L, 8L, 12L, 16L, 1L, 5L, 
9L, 13L, 2L, 6L, 10L, 14L, 3L, 7L, 11L, 15L, 4L, 8L, 12L, 16L, 
1L, 5L, 9L, 13L, 2L, 6L, 10L, 14L, 3L, 7L, 11L, 15L, 4L, 8L, 
12L, 16L, 1L, 5L, 9L, 13L, 2L, 6L, 10L, 14L, 3L, 7L, 11L, 15L, 
4L, 8L, 12L, 16L), levels = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, 
&quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;11&quot;, &quot;12&quot;, &quot;13&quot;, &quot;14&quot;, &quot;15&quot;, &quot;16&quot;), class = &quot;factor&quot;), 
Time = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c(&quot;May 21&quot;, 
&quot;July 21&quot;, &quot;July 22&quot;), class = &quot;factor&quot;), Fraction = c(&quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Fine earth&quot;, 
&quot;Fine earth&quot;, &quot;Fine earth&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, &quot;Plant remains&quot;, 
&quot;Plant remains&quot;, &quot;Plant remains&quot;), Corg = c(2.242, 2.3539374, 
2.24021216, 2.29317597, 2.02245578, 2.19354698, 2.25056174, 
1.97073662, 2.03775086, 2.182, 2.054, 2.23143852, 2.43, 1.963, 
1.95676108, 1.91028975, 2.46682164, 2.17334891, 1.95329696, 
1.92659278, 2.49012358, 2.29460285, 2.14150798, 2.0982069, 
3.20281214, 3.31694401, 2.94037862, 2.86813115, 2.71119738, 
2.2551189, 2.58737415, 2.52201226, 2.568840791, 2.268484741, 
2.276562231, 2.232093049, 2.629438029, 2.130462962, 2.181554457, 
2.441597329, 2.677526322, 2.793236578, 2.675139082, 2.446825908, 
2.38063425, 2.452040834, 2.214548263, 2.020306975, 30.58, 
47.627, 39.743, 40.186, 29.493, 36.148, 35.165, 36.891, 19.502, 
34.743, 41.201, 36.061, 38.791, 27.523, 41.801, 45.271, 40.159, 
41.955, 36.073, 38.769, 44.57, 39.843, 42.192, 44.747, 45.983, 
47.563, 44.659, 49.04, 49.658, 45.392, 48.546, 42.873, 41.302, 
45.665, 23.643, 32.305, 27.708, 38.898, 32.446, 32.134, 46.13, 
34.115, 44.719, 46.675, 37.619, 46.065, 39.547, 38.268)

I've tried with the function ggbarplot () but I find it impossible to have more than 1 grouping variable and I have to drop one.

library(ggpubr)
library(rstatix)
res.stats &lt;- datamay %&gt;%
group_by(Treatment) %&gt;%
t_test(C..org ~ Fraction) %&gt;%
adjust_pvalue() %&gt;%
add_significance()
res.stats
plot&lt;- ggbarplot(datamay, x = &quot;Treatment&quot;,
y = &quot;Corg&quot;, add = &quot;mean_se&quot;, add.params=list(color = &quot;black&quot;),
color =&quot;Fraction&quot;, fill= &quot;Fraction&quot;,
palette=c(&quot;darkgoldenrod4&quot;, &quot;darkolivegreen4&quot;),
ylim=c(0,60))
plot

Also, I have tried with ggplot2 dropping Treatment as the grouping variable but I can't manage to get the error bars where they shoud be.

library(ggplot2)
library(Rmisc)
summdata &lt;- summarySE(datas, measurevar=&quot;Corg&quot;, groupvars=c(&quot;Time&quot;, &quot;Treatment&quot;,&quot;Fraction&quot;), na.omit(TRUE))
plot&lt;-ggplot(summdata, aes(x=Time, y=Corg, fill=Fraction)) + 
geom_bar(stat=&quot;identity&quot;) +
geom_errorbar(aes(ymin=Corg-se, ymax=Corg+se),
width=.2,                    
position=position_dodge(.9))
plot

Is it even possible to plot a stacked bar plot with two grouping variables? Any help will be appreciated.

Thanks in advance and let me know if you need any more information about the dataset.

答案1

得分: 0

I think it would be easiest to summarize your data frame using dplyr to get the mean values and error bars, then plot that. You can use facets to show the effects of different treatments. Your request to stack bars is possible, but if you also want error bars this is very problematic from a data visualization point of view, and you should stick to dodged rather than stacked bars:

library(tidyverse)

datas %>%
  group_by(Time, Fraction, Treatment) %>%
  summarise(se = sd(Corg)/sqrt(n()),
            Corg = mean(Corg),
            upper = Corg + 1.96 * se,
            lower = Corg - 1.96 * se) %>%
  ggplot(aes(Time, Corg, fill = Fraction)) + 
  geom_col(position = position_dodge()) +
  geom_point(position = position_dodge(1), alpha = 0.2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.5, 
                position = position_dodge(width = 1), alpha = 0.3) +
  facet_grid(.~Treatment) +
  theme_minimal() +
  scale_fill_manual(values = c('deepskyblue4', 'orange'))

Stacked bars with 2 groups and error bars

英文:

I think it would be easiest to summarize your data frame using dplyr to get the mean values and error bars, then plot that. You can use facets to show the effects of different treatments. Your request to stack bars is possible, but if you also want error bars this is very problematic from a data visualization point of view, and you should stick to dodged rather than stacked bars:

library(tidyverse)

datas %&gt;%
  group_by(Time, Fraction, Treatment) %&gt;%
  summarise(se = sd(Corg)/sqrt(n()),
            Corg = mean(Corg),
            upper = Corg + 1.96 * se,
            lower = Corg - 1.96 * se) %&gt;%
  ggplot(aes(Time, Corg, fill = Fraction)) + 
  geom_col(position = position_dodge()) +
  geom_point(position = position_dodge(1), alpha = 0.2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.5, 
                position = position_dodge(width = 1), alpha = 0.3) +
  facet_grid(.~Treatment) +
  theme_minimal() +
  scale_fill_manual(values = c(&#39;deepskyblue4&#39;, &#39;orange&#39;))

Stacked bars with 2 groups and error bars

huangapple
  • 本文由 发表于 2023年5月25日 20:06:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定