ggplot2 shadings and multiple data points in one plot

Viewed 125

I have measured the emission of two compounds acetone and acetaldehyde nine times across 116 days. I made a graph like this:

enter image description here

The measurements are divided into three groups: a, b and c.

I'm trying to plot the average (and standard error) of group b and c in the graph as well. This symbol would have to be different.

Any ideas?

The data looks like this

df <- structure(list(compound = c("Acetaldehyde", "Acetaldehyde", "Acetaldehyde", 
"Acetaldehyde", "Acetaldehyde", "Acetaldehyde", "Acetaldehyde", 
"Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone", "Acetone", 
"Acetone", "Acetone", "Acetone", "Acetone", "Acetone", "Acetone"
), days = c(0, 4, 10, 17, 24, 66, 81, 94, 116, 0, 4, 10, 17, 
24, 66, 81, 94, 116), emission = c(26.13, 59.09, 45, 11.41, 6.71, 
0.07, 0, 0.53, 0.56, 28.47, 46.65, 44.03, 56.68, 29.08, 2.73, 
7.72, 0.38, 0.21), se = c(5.88, 11.04, 11.4, 4.31, 3.54, 0.03, 
0, 0.25, 0.31, 7.44, 8.98, 9.92, 22.79, 13.83, 1.2, 6.38, 0.09, 
0.07), group = c("a", "b", "b", "b", "b", "b", "c", "c", "c", 
"a", "b", "b", "b", "b", "b", "c", "c", "c")), row.names = c(NA, 
-18L), class = "data.frame")

and the code for my graph looks like this:

df%>%
  ggplot(aes(x=days, y=emission))+
  geom_point(color="darkblue", size=3.5) +
  geom_errorbar(aes(ymin=emission, ymax=emission+se), width=.2,
                position=position_dodge(0.05)) +
  labs(color = "", size= "") +
  labs(x = "Incubation time (days)", y = "Production (umol g-1 dw soil h-1)") +
  theme_bw() +
  facet_wrap(vars(compound), scales = "free_y")

---------------------------------UPDATE---------------------------------

I can see I did a terrible job trying to explain what I'm trying to do.

The data has a column called group. In this column the measurements are divided into a, b and c. I want to plot the average of each of these groups on top of the plot I've showed (where all measurements. There is only one measurement in a, so it does not make sense for this group. However for group b and c I wish to plot the average (and standard error) in the graph as well. These symbols would of course need to be different from what I've already plotted.

Also, I'm trying to change the color of the background behind each of the groups, to have different colors similar to this figure I found on google:

enter image description here.

1 Answers

I edited my answer:

library(ggplot2)
library(magrittr)
library(plotrix)
library(knitr)
library(dplyr)

#creating group b acetaldehyde mean and standart error
b_mean_ald <- mean(df$emission[df$group == "b" & df$compound == "Acetaldehyde"])
b_se_ald <- std.error(df$emission[df$group == "b" & df$compound == "Acetaldehyde"])

#creating group b acetone mean and standart error
b_mean_acet <- mean(df$emission[df$group == "b" & df$compound == "Acetone"])
b_se_acet <- std.error(df$emission[df$group == "b" & df$compound == "Acetone"])

#creating group c acetaldehyde mean and standart error
c_mean_ald <- mean(df$emission[df$group == "c" & df$compound == "Acetaldehyde"])
c_se_ald <- std.error(df$emission[df$group == "c" & df$compound == "Acetaldehyde"])

#creating group c acetone mean and standart error
c_mean_acet <- mean(df$emission[df$group == "c" & df$compound == "Acetone"])
c_se_acet <- std.error(df$emission[df$group == "c" & df$compound == "Acetone"])


# making a dataframe which contains mean and se of each compounds per groups
mean_df <- data.frame(
  compound = c("Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone"),
  days = c(33, 95, 33, 95),
  emission = c(b_mean_ald, c_mean_ald, b_mean_acet, c_mean_acet),
  se = c(b_se_ald, c_se_ald, b_se_acet, c_se_acet), 
  group = c("b", "c", "b", "c")
) 


That is the final df(df_fin) looks like:

enter image description here


Now we apply it in the ggplot:
#final plot
df_fin%>%
  ggplot(aes(x=days, y=emission))+
  labs(color = "", size= "") +
  labs(x = "Incubation time (days)", y = "Production (umol g-1 dw soil h-1)") +
  facet_wrap(vars(compound), scales = "free_y") +
  
  geom_rect( xmin = 2.2, xmax = 69, ymin = -5, ymax = 85,
             alpha = 0.05, fill="green") +
  geom_rect( xmin = 69, xmax = 125, ymin = -5, ymax = 85,
             alpha = 0.05, fill="yellow")+
  geom_rect( xmin = -10, xmax = 2.2, ymin = -5, ymax = 85,
             alpha = 0.05, fill="purple")+
  geom_pointrange(aes(x = days, y = emission, ymin = emission, ymax = emission + se, color = category, shape = category, group = category),
                  size = 0.5) +
  scale_color_manual(name = "", labels = c("Period Mean", "Emission"), 
                     values = c("red", "darkblue")) +
  scale_shape_manual(name = "", labels = c("Period Mean", "Emission"), values = c(17, 19))+

  theme_bw() 

The final plot:

enter image description here


If you want to include groups in legend also you should do a little tricky:
#final plot with groups and measurments legend
df_fin%>%
  ggplot(aes(x=days, y=emission))+
  labs(color = "", size= "") +
  labs(x = "Incubation time (days)", y = "Production (umol g-1 dw soil h-1)") +
  facet_wrap(vars(compound), scales = "free_y") +
  geom_rect(xmin = -2, xmax = 2.2, ymin = 85, ymax = 100,
            fill="green",alpha = 0.05)+
  geom_rect(xmin = 2.2, xmax = 69, ymin = 85, ymax = 100,
            fill = "yellow", alpha = 0.05) +
  geom_rect(xmin = 69, xmax = 120, ymin = 85, ymax = 100,
            fill="purple",alpha = 0.05) +
  geom_rect(xmin = -2, xmax = 2.2, ymin = -20, ymax = 0,
            fill="green",alpha = 0.05)+
  geom_rect(xmin = 2.2, xmax = 69, ymin = -20, ymax = 0,
            fill = "yellow", alpha = 0.05) +
  geom_rect(xmin = 69, xmax = 120, ymin = -20, ymax = 0,
            fill="purple",alpha = 0.05) +
  geom_rect(aes(xmin = -2, xmax = 2.2, ymin = 0, ymax = 85,
              fill="A", alpha = "D"))+
  geom_rect(aes(xmin = 2.2, xmax = 69, ymin = 0, ymax = 85,
             fill = "B", alpha = "E")) +
  geom_rect(aes(xmin = 69, xmax = 120, ymin = 0, ymax = 85,
              fill="C",alpha = "F"))+
  
  geom_pointrange(aes(x = days, y = emission, ymin = emission, ymax = emission + se, color = category, shape = category, group = category),
                  size = 0.5) +
  scale_color_manual(name = "Measurments", labels = c("Period Mean", "Emission"), 
                     values = c("red", "darkblue")) +
  scale_fill_manual(name = "Groups", labels = c("A", "B","C"), values = c("green", "yellow","purple"))+
  scale_shape_manual(name = "Measurments", labels = c("Period Mean", "Emission"), values = c(17, 19))+
  scale_alpha_manual(name = "Groups", labels = c("A", "B", "C"), values = c(0.05,0.05,0.05)) +
  guides(fill = guide_legend(override.aes = list(alpha = 1)))


Plot with groups and measurments legend:
enter image description here
In the last plot, because small rectangle of legends with alpha values are not visualized well, we override the legend alpha without affecting main plot alpha, by guides() function in the end of the plot code
So the legends of the groups have more alpha than the main plot.
In this plot, intentionally I set the alpha of the legend groups to 1 and alpha of the plot to 0.05 to show their difference. You can set any alpha to each of them to make them more similar
The plot below shows simingly the similar alpha of the legend and plot: (by setting plot alpha to 0.05 and legend alpha to 0.2)
enter image description here

---------
You can also add theme_bw() at the end of the last plot to create border (I did not include it in the last plot)

Related