ggh4x icon indicating copy to clipboard operation
ggh4x copied to clipboard

A problem I met about ggh4x after its update from v 0.1.2.1 to v 0.2.1

Open DengEr-1993 opened this issue 2 years ago • 1 comments

I draw a plot using ggplot2 and ggh4x with the help from @teunbrand. And it works perfect in ggh4x old version 0.1.2.1. A key function was from the author and it helped me a lot.

But several days ago, I updated some pacages including the ggh4x and now it is v 0.2.1.

Then the plot can not be produced normally with the error: Error in strip$grobs[is_strip][1] : 下标出界(subscript out of bounds)

So I think it may work if I downgrade the ggh4x version to the original version 0.1.2.1.

However the problem stil exists. I haven't changed my data so I want to know if there is something wrong with the ggh4x ?

######## here is the original question: Can I adjust the fill(color) of different label regions when using ggh4x package

Here is the code:

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")
#####
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)

So I hope @teunbrand could help me. vary thankful.

DengEr-1993 avatar May 29 '22 13:05 DengEr-1993

In the new version, it should be easier to create varying fills for the facet strips. The facet functions have a strip argument, where you can give a strip_*(). For strip_themed() and strip_nested() you can vary the background fills.

library(ggplot2)
library(ggh4x)

# data<-structure(...) # omitted for brevity

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,
    strip = strip_nested(
      background_x = elem_list_rect(fill = rainbow(15))
    ))+
  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")

teunbrand avatar May 29 '22 14:05 teunbrand

I'm considering this issue resolved.

teunbrand avatar Aug 13 '22 07:08 teunbrand