Home > Software engineering >  using "overall" groups after facet_wrap (gepolt)
using "overall" groups after facet_wrap (gepolt)

Time:09-27

I want to add a bar (with significance) to my ggplot. I know the code for adding the bar (stat_pvalue_manual()) but my problem is of more general nature. Is it possible to use "overall" groups after a facet_wrap?

My code is the following:

library(data.table)
library(ggplot2)
library(dplyr)
data(mtcars)
data <-mtcars[,c(1,2)]
data <- data[c(1:30),]
setDT(data, keep.rownames = TRUE)[]
data <- as.data.frame(data[,c(1,2)])
data$group <- c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3)
data$group2 <- c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
data$mpg2 <- data$mpg * 0.6345
data$mp3 <- data$mpg * 0.7564
data$group <- as.factor(data$group)
data$group2 <- as.factor(data$group2)


long_data <- melt(setDT(data), id.vars = c("rn", "group", "group2"), variable.name = "Timepoint")

p <- ggplot(long_data, aes(x = Timepoint, y = value, fill = group2))  
  geom_bar(stat = "summary", fun = "mean", position = "dodge", na.rm = T, alpha=0.35)  
  geom_point(data = long_data %>% filter(group=="1") , aes(x = Timepoint, y = value, colour =  group),  size = 0.9, shape = 16) 
  geom_point(data = long_data %>% filter(group=="2") ,aes(x = Timepoint, y = value, colour =  group),  size = 0.9, shape = 16) 
  geom_point(data = long_data %>% filter(group=="3") ,aes(x = Timepoint, y = value, colour =  group),  size = 0.9, shape = 17) 
  geom_line(data = long_data, aes(x= Timepoint, y = value, group = rn),  alpha = .3, size = 0.4)  
  facet_wrap(~group2)



p

I want to "use" both groups and add a bar between group 1 and 2 (which are separated by the facet_wrap). I attach a picture of what the plot gives me and manually draw in red what I would want.

enter image description here

CodePudding user response:

One way round this problem is to overlay two plots on top of each other using patchwork. One plot will have to have only a geom_signif layer, the other is your existing plot:

library(ggpubr)
library(patchwork)

p1 <- ggplot(long_data, aes(x = Timepoint, y = value, colour = group))  
  geom_bar(aes(fill = group2), color = NA, stat = "summary", fun = "mean", 
           position = "dodge", na.rm = TRUE, alpha = 0.35)  
  geom_point(data = filter(long_data, group == "1"), size = 0.9, shape = 16)  
  geom_point(data = filter(long_data, group == "2"), size = 0.9, shape = 16)  
  geom_point(data = filter(long_data, group == "3"), size = 0.9, shape = 17)  
  geom_line(data = long_data, aes(group = rn), color = 'black',
            alpha = 0.3, size = 0.4)  
  facet_wrap(~group2)  
  ylim(c(0, 40))

p2 <- ggplot(long_data, aes(group2, value))  
  geom_bar(stat = 'summary', fun = 'mean', fill = NA)  
  geom_signif(comparisons = list(c('1', '2')), test = 't.test',
              map_signif_level = function(p) sprintf("p = %.2g", p),
              extend_line = 0.025, y_position = 37, vjust = -0.2)  
  ylim(c(0, 40))  
  theme_void()

Now we draw p1 and put p2 directly on top of it:

layout <- c(
  area(t = 1, l = 1, b = 2, r = 2),
  area(t = 1, l = 1, b = 2, r = 2)
)

p1   p2   plot_layout(design = layout) 

enter image description here

CodePudding user response:

As an alternative to the significance bar, perhaps it would be easier and just as clear to add bars for the overall group means in the two facets, such as geom_crossbar(aes(group = group2), color = "black"). Then the overlap of the error bars can signal the size of the differences across panels.

  • Related