Here is my sample data and code:
Now I wanna adjust the blank region to different colors. And I used the ggh4x package () and its vital function: facet_nested(.~Organ+Disease, scales="free",switch = "x",nest_line = TRUE)
data<-structure(list(Name = structure(53:68, .Label = c("1_control",
"1_CCM(Kit1 ko)", "2_control", "2_CCM(Pdcd20 ko)", "3_control",
"3_1 day", "3_3 days", "3_1 month", "4_control", "4_1 day", "4_3 days",
"4_1 month", "5_control", "5_1 day", "5_3 days", "5_1 month",
"6_control", "6_TBI(1 day)", "7_control", "7_TBI(3 day)", "8_control",
"8_TBI(1 month)", "9_control", "9_VEGF", "10_control", "10_VEGF",
"11_Brain Healty", "12_control_1", "12_control_2", "12_AOD(Jnk1/2/3 ko)",
"13_control", "13_Cpt1_ko(Cdh5 driven)", "14_control", "14_Tsc2ko(Tbx4 driven)",
"15_control", "15_Zmpste24 ko", "16_control", "16_Adrenomedullin\nko(Cdh5 driven)",
"17_Lung Healthy", "18_control(14w)", "18_carboplatin(14w)",
"18_radiation(14w)", "18_6 weeks", "18_70 weeks", "19_ECs(bone marrow)",
"20_control", "20_diabetic nephropathy", "21_control", "21_Alport syndrome",
"22_control", "22_Alport syndrome", "23_Kidney Healthy", "24_control",
"24_Differentiated", "25_Heart Healthy", "26_control", "26_Tumor",
"27_control", "27_Nonalcoholic\nsteatohepatitis", "28_Liver Healthy",
"29_control", "29_autoimmune uveitis", "30_control", "30_facioscapulohumeral\nmuscular dystrophy",
"31_control", "31_RANKL ko", "32_control", "32_Notch1 +/vg",
"32_Notch1 +/12", "32_Notch1 +/-", "32_Notch1 -/12"), class = "factor"),
Disease = structure(c(24L, 24L, 25L, 26L, 26L, 27L, 27L,
28L, 29L, 29L, 30L, 30L, 31L, 31L, 32L, 32L), .Label = c("CCM\nmodel1",
"CCM\nmodel2", "Epilepsy", "EAE", "Stroke", "TBI\n1day",
"TBI\n3day", "TBI\n1month", "VEGF\nsti_DG\nregion", "VEGF\nsti_CA1\nregion",
"Healthy\n(Brain)", "AOD", "CPT1\nko", "Tsc2\nko", "Zmpste\n_24 ko",
"AM\nko", "Healthy\n(Lung)", "Chemo\nRadio\n(Tibiae)", "ECs\n(BM)",
"diabetic\nnephro\n_pathy", "Alport\nsyndrome\nEC\npopulation1",
"Alport\nsyndrome\nEC\npopulation2", "Healthy\n(Kidney)",
"Aorta", "Healthy\n(Heart)", "Tumor", "NASH", "Healthy\n(Liver)",
"EAU", "FSHD", "RANKL\nko", "Notch1\nmodified"), class = "factor"),
Organ = structure(c(5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L,
8L, 8L, 9L, 9L, 10L, 10L), .Label = c("Brain", "Lung", "Bone",
"Kidney", "Aorta", "Liver", "Retina", "Mus", "Lymp", "Embryo"
), class = "factor"), fill = c("#FFFFFF", "#FFFF33", "#FFFFFF",
"#FFFFFF", "#00F5FF", "#FFFFFF", "#00F5FF", "#FFFFFF", "#FFFFFF",
"#666666", "#FFFFFF", "#7FC97F", "#FFFFFF", "#BEAED4", "#FFFFFF",
"#A6D854"), Condition = c("#FFFFFF", "#FFFFFF", "#FFFFFF",
"#CCCCFF", "#CCCCFF", "#CCCCFF", "#CCCCFF", "#CCCCFF", "#CCCCFF",
"#CCCCFF", "#CCCCFF", "#CCCCFF", "#fbb03b", "#fbb03b", "#fbb03b",
"#fbb03b"), Organ_fill = c("#FFFF33", "#FFFF33", "#FFFF33",
"#00F5FF", "#00F5FF", "#00F5FF", "#00F5FF", "#00F5FF", "#666666",
"#666666", "#7FC97F", "#7FC97F", "#BEAED4", "#BEAED4", "#A6D854",
"#A6D854"), mean = c(858.98750157615, 420.38122185754, 262.590442373838,
185.670810881339, 233.452076898716, 3219.28563479013, 2427.32556269574,
1552.81582140235, 394.765393523994, 420.454351698853, 1295.47692129111,
1357.53823242958, 1352.71703262044, 1319.89488759262, 4.78327819764262,
6.86013366170554), sd = c(55.535911372066, 15.5347305073907,
29.5537795773611, 17.4390137323828, 36.0381883614951, 641.298872189499,
338.296364941458, 254.748027714364, 17.1375059099585, 100.491982520438,
49.0693043847785, 191.034062908029, 105.031407429784, 185.916546617784,
1.21846060820158, 3.38993644072368)), row.names = 53:68, class = "data.frame")
# 2021年04月08日19:35:04
ggplot(data = data, aes(Name,mean, label = Name,fill=Organ)) +
geom_bar(position="dodge2", stat="identity",width = 0.85,color="black") +
geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),position = position_dodge(0.85), width = .2) +
facet_nested(.~Organ+Disease, scales="free",switch = "x",nest_line = TRUE)+
### facet_wrap(strip.position="bottom")+
theme_classic2() +
theme(
plot.title = element_text(hjust = 0.5),
plot.margin = unit(c(5, 10, 10, 10), "mm"),
strip.background = element_rect(colour="black", fill="white"),
## ggh4x.facet.nestline = element_line(colour = "blue"),
strip.text.x = element_text(size = 6, angle=0),
strip.placement = "outside")
I don't know if there is related function in ggh4x package.
I hope somebody could helpe me.
There is no nice function to do this, but you can manually change the colours of the strips if you want to get your hands dirty with some gtable
g <- ggplot(data = data, aes(Name,mean, label = Name,fill=Organ)) +
geom_bar(position="dodge2", stat="identity",width = 0.85,color="black") +
geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),position = position_dodge(0.85), width = .2) +
facet_nested(.~Organ+Disease, scales="free",switch = "x",nest_line = TRUE)+
theme_classic() +
theme(
plot.title = element_text(hjust = 0.5),
plot.margin = unit(c(5, 10, 10, 10), "mm"),
strip.background = element_rect(colour="black", fill="white"),
strip.text.x = element_text(size = 6, angle=0),
strip.placement = "outside")
# Convert to gtable
gt <- ggplotGrob(g)
assign_strip_colours <- function(gt, index, colours) {
if (length(index) != length(colours))
stop()
# Decide which strips to recolour, here: the first 3
is_strips <- which(startsWith(gt$layout$name, "strip-b"))[index]
# Extract strips
strips <- gt$grobs[is_strips]
# Loop over strips
strips <- mapply(function(strip, colour) {
# Find actual strip
is_strip <- strip$layout$name == "strip"
grob <- strip$grobs[is_strip][[1]]
# Find rectangle
is_rect <- which(vapply(grob$children, inherits, logical(1), "rect"))
# Change colour
grob$children[[is_rect]]$gp$fill <- colour
# Put back into strip
strip$grobs[is_strip][[1]] <- grob
return(strip)
}, strip = strips, colour = colours)
# Put strips back into gtable
gt$grobs[is_strips] <- strips
return(gt)
}
gt <- assign_strip_colours(gt, 1:15, rainbow(15))
# Plot gtable
grid::grid.newpage(); grid::grid.draw(gt)