I want to use "facet_wrap" to have several violin plots in one plot. I have 2 groups of variables, one is "color" and then within each color, there are different "groups". When I do the following, I can almost get what I want:
sample data:
score<-c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
color<-c("white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","black","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","grey","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown","brown")
group<-c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","D","D","D","D","D","D","D","D","D","E","E","E","E","E","F","F","F","F","F","F","F","F","F","F","F","F","F","F","F","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","H","H","H","H","H","H","H","H","H","H","H","H","H","H","H","I","I","I","I","I","I","I","I","I","I","I","I","I","I","I")
animal<-c("dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","dog","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","cat","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny")
df<-tibble(animal,score,group,color)
making the plot:
ggplot(df,aes(x=animal,y=score,fill=animal))
geom_violin()
facet_wrap(color ~ group)
theme_clean()
However, I would like all of the plots with the same value for "color" to be on the same line, and for it to just say the name of each color once, like as a subheading, at the top of that group of letters(instead of above each letter). Does anyone know if this is possible?
CodePudding user response:
If you know the layout of the plot in advance, you can use ggh4x::facet_manual()
. Disclaimer: I'm the author of ggh4x.
score <- rep(1:10, 12)
color <- rep(c("white", "black", "grey", "brown"), each = 30)
group <- rep(c("A", "B", "C", "D", "E", "F", "G", "H", "I"),
c(15, 15, 16, 9, 5, 15, 15, 15, 15))
animal <- rep(c("dog", "cat", "bunny"), c(39, 46, 35))
df <- data.frame(animal, score, group, color)
library(ggh4x)
#> Loading required package: ggplot2
layout <- "
ABC
DE#
FG#
HI#
"
ggplot(df, aes(animal, score, fill = animal))
geom_violin()
facet_manual(
vars(color, group),
design = layout,
strip = strip_nested()
)
Created on 2022-08-31 by the reprex package (v2.0.0)