Home > Back-end >  Is there a way to facet a line graph but add a constant line in R?
Is there a way to facet a line graph but add a constant line in R?

Time:07-12

I am making a line graph using ggplot of some data that I have were I am sizing objects based on treatment groups into bins and looking at the percentage in each size bin by group.

My goal is to facet the data but have the control line as a graph but also added to the other groups when they facet.

I have the graph working were I get all of my groups one of which is my control and I can facet it. I just would to keep the control line on the subsequent graphs but slightly grayed out. I am not sure how to add that.

graph <- ggplot(data=my_data, aes(x=bins, y=p_per_bin, group = group, 
                                        color = group, linetype = group, shape = group)) 
     stat_summary(fun = "mean", geom = "line", lwd = rel(1)) 
     stat_summary(fun = mean,
                  geom = "pointrange",
                  fun.max = function(x) mean(x)   sd(x) / sqrt(length(x)),
                  fun.min = function(x) mean(x) - sd(x) / sqrt(length(x))) 
     stat_summary(fun = "mean", geom = "point", size = rel(2), fill = "white", stroke = rel(1.1))
   
   graph   facet_wrap(~group) 

Here is a portion of my data as a sample. Group "a" is the control.

sample <- c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9)
group <- c("a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b","b",
           "b","c","c","c","c","c","c","c","c","c")
bins <-c("0-20","20-40","40-60","0-20","20-40","40-60","0-20","20-40","40-60","0-20","20-40","40-60", "0-20","20-40","40-60", "0-20","20-40","40-60", "0-20","20-40","40-60", "0-20","20-40","40-60", "0-20","20-40","40-60")
p_per_bin <- c(0,37.7192982,21.0526316,0,36.744186,23.7209302,0,36.2126246,31.5614618,
0,31.25,27.0833333,0,41.2280702,28.5087719,0,39.6078431,31.372549,0,43.7262357,
20.1520913,0,35.4716981,21.1320755,0,38.5350318, 29.9363057)
my_data <- cbind(sample,group,bins,p_per_bin)

CodePudding user response:

One option would be to add your reference group via an additional stat_summary for which you only use the data on the reference group. To get this layer displayed on each facet it's important to drop the group column after filtering.

For the example code I have chosen group "a" as the reference group:

library(ggplot2)
library(dplyr)

ggplot(data = my_data, aes(
  x = bins, y = p_per_bin, group = group,
  color = group, linetype = group, shape = group
))  
  #### Add line for reference group
  stat_summary(fun = "mean", geom = "line", lwd = rel(.5), 
               data = ~filter(.x, group == "a") |> select(-group), 
               color = "grey45", linetype = "solid")  
  ####
  stat_summary(fun = "mean", geom = "line", lwd = rel(1))  
  stat_summary(
    fun = mean,
    geom = "pointrange",
    fun.max = function(x) mean(x)   sd(x) / sqrt(length(x)),
    fun.min = function(x) mean(x) - sd(x) / sqrt(length(x))
  )  
  stat_summary(fun = "mean", geom = "point", size = rel(2), fill = "white", stroke = rel(1.1))  
  facet_wrap(~group)

  • Related