rggplot2facetggh4x

About ggh4x and faceting : the labels' location and color change of different regions


Here is my sample data and code:

data<-structure(list(Organ = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                         2L, 2L, 2L), .Label = c("Brain\n(GSE123968)", "Brain\n(GSE95401)", 
                                                                 "Brain\n(E-MTAB-6910)", "Lung\nA_(GSE71159)\nC_(E-MTAB-6595)\nL_(GSE139818)\nH_(GSE137501)\nC_(GSE84345)", 
                                                                 "Bone\nT_(E-MTAB-6872)\nP_(E-GEOD-61636)", "Embryo\n(E-GEOD\n-69276)", 
                                                                 "Kidney\nD_(GSE123177)\nA_(GSE135442)", "Aorta\n(E-MTAB\n-7148)", 
                                                                 "Liver\nTumor_\n(E-MTAB\n-4842)\nNASH_\n(GSE119340)", "Retina\n(GSE\n144168)", 
                                                                 "Mus\n(GSE\n14\n15\n62)", "Lym\n(GSE\n12\n93\n15)"), class = "factor"), 
                     Name = structure(1:10, .Label = c("1_Control", "1_Krit1", 
                                                       "2_Control", "2_Pdcd20", "3_Control", "3_Acute", "3_Subacute", 
                                                       "3_Chronic", "4_Control", "4_Acute", "4_Subacute", "4_Chronic", 
                                                       "5_Control", "5_Acute", "5_Subacute", "5_Chronic", "6_Control_acute", 
                                                       "6_Acute", "7_Control_subacute", "7_Subacute", "8_Control_chronic", 
                                                       "8_Chronic", "9_Control_DG", "9_DG_VEGF", "10_Control_CA1", 
                                                       "10_CA1_VEGF", "11_Control_Ectrl", "11_Control_EfCtrl", "11_E3KO", 
                                                       "12_Control", "12_Cpt1_ko", "13_Control", "13_Tsc2_ko", "14_Control", 
                                                       "14_Zmpste24_ko", "15_Control", "15_AM_ko", "16_Control", 
                                                       "16_Chemotherapy", "16_Radiation", "16_Young", "16_Aged", 
                                                       "17_EC in bone", "18_Control", "18_N1_plus_vg", "18_N1_plus_12", 
                                                       "18_N1_plus_-", "18_N1_12_-", "19_Control", "19_STZ", "20_Control", 
                                                       "20_AS_dimtdT", "21_Control", "21_AS_brighttdT", "22_Control", 
                                                       "22_Differentiated", "23_Control", "23_Liver  tumor", "24_Control", 
                                                       "24_Nash", "25_Control", "25_EAU", "26_Control", "26_ iDUX4", 
                                                       "27_Control", "27_RANKL_ko"), class = "factor"), Disease = structure(c(1L, 
                                                                                                                              1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("CCM", "Seizure", 
                                                                                                                                                                              "EAE", "Stroke", "TBI", "Infla", "AOD", "CVD", "LAM", "HGPS", 
                                                                                                                                                                              "Tumor", "Pancy", "Carcino", "DK", "AS", "Home", "NASH", 
                                                                                                                                                                              "EAU", "FSHD"), class = "factor"), fill = c("#FF0000", "#FF0000", 
                                                                                                                                                                                                                          "#00FF00", "#00FF00", "#7570B3", "#7570B3", "#7570B3", "#7570B3", 
                                                                                                                                                                                                                          "#A6D854", "#A6D854"), mean = c(806.898912005764, 837.595854895274, 
                                                                                                                                                                                                                                                          898.32650562378, 2208.81852805305, 212.969188902024, 195.692360171933, 
                                                                                                                                                                                                                                                          181.622170748826, 454.592532593114, 477.034742547228, 383.52324684751
                                                                                                                                                                                                                          ), sd = c(108.62191806741, 86.1336851228738, 108.18221445462, 
                                                                                                                                                                                                                                    551.492848636393, 35.6423740115715, 28.0730270395565, 42.7691784348475, 
                                                                                                                                                                                                                                    116.005320530673, 43.8700365938649, 63.2689449323908)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                          10L), class = "data.frame")
data
##  2021年04月08日19:35:04

ggplot(data = data, aes(Name,mean, label = Name)) +
  geom_bar(position="dodge2", stat="identity",width = 0.85,fill=data$Name,color="black") +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),position = position_dodge(0.85), width = .2) +
  facet_nested(.~Organ+Disease, scales="free_x",switch = "x",nest_line = TRUE)+
  ###  facet_wrap(strip.position="bottom")+
  theme_classic2() +
  theme(legend.position = "none",
        plot.title = element_text(hjust = 0.5),
        plot.margin = unit(c(5, 10, 10, 10), "mm"),
        strip.background = element_rect(colour="black", fill="#CCCCFF"),
##        ggh4x.facet.nestline = element_line(colour = "blue"),
        strip.text.x = element_text(size = 6, angle=0))

I now have some questions.

First: I wanna change the color of different label region. I don't know if I can set different colors in different regions? enter image description here

My second question:

If I can move the class label to x axis bottom after the every bar labels. I know it can be top. enter image description here

Vary thankful.


Solution

  • My second question:

    If I can move the class label to x axis bottom after the every bar labels. I know it can be top.

    I solved it.There is parameter in theme can be used to change the location of this label:

    **strip.placement = "outside".**
    
    
    ggplot(data = data, aes(Name,mean, label = Name)) +
      geom_bar(position="dodge2", stat="identity",width = 0.85,fill=data$Name,color="black") +
      geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),position = position_dodge(0.85), width = .2) +
      facet_nested(.~Organ+Disease, scales="free_x",switch = "x",nest_line = TRUE)+
      ###  facet_wrap(strip.position="bottom")+
      theme_classic2() +
      theme(legend.position = "none",
            plot.title = element_text(hjust = 0.5),
            plot.margin = unit(c(5, 10, 10, 10), "mm"),
            strip.background = element_rect(colour="black", fill="#CCCCFF"),
    ##        ggh4x.facet.nestline = element_line(colour = "blue"),
            strip.text.x = element_text(size = 6, angle=0),
            strip.placement = "outside")
    

    enter image description here

    Who can answer my first question.