Change the label font size but also changed the x axia labels order when using ggtext

72 views Asked by At

My first question is not completely solved by @teunbrand.

He gave me a piece of advice but I'm confused by it.

The x axis label is out of order after usting the big_label_md function though I get what I need.

Now I want to get the right label order. The biggest problem is that if I just change one label font size the bottom label will keep the right order as before. However when I modify two or more(view my code) labels font size, all the labels will be out of order.

Here is my reproducible data and code:

library(ggplot2)
library(ggtext)

data<-structure(list(Name = structure(58: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(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(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", "#00F5FF", "#FFFFFF", "#FFFFFF", "#666666", 
                              "#FFFFFF", "#7FC97F", "#FFFFFF", "#BEAED4", "#FFFFFF", "#A6D854"
                     ), Condition = c("#CCCCFF", "#CCCCFF", "#CCCCFF", "#CCCCFF", 
                                      "#CCCCFF", "#CCCCFF", "#CCCCFF", "#fbb03b", "#fbb03b", "#fbb03b", 
                                      "#fbb03b"), Organ_fill = c("#00F5FF", "#00F5FF", "#00F5FF", 
                                                                 "#666666", "#666666", "#7FC97F", "#7FC97F", "#BEAED4", "#BEAED4", 
                                                                 "#A6D854", "#A6D854"), mean = c(3219.28563479013, 2427.32556269574, 
                                                                                                 1552.81582140235, 394.765393523994, 420.454351698853, 1295.47692129111, 
                                                                                                 1357.53823242958, 1352.71703262044, 1319.89488759262, 4.78327819764262, 
                                                                                                 6.86013366170554), sd = c(641.298872189499, 338.296364941458, 
                                                                                                                           254.748027714364, 17.1375059099585, 100.491982520438, 49.0693043847785, 
                                                                                                                           191.034062908029, 105.031407429784, 185.916546617784, 1.21846060820158, 
                                                                                                                           3.38993644072368)), row.names = 58:68, class = "data.frame")



data
########

    big_label_md <- function(x, label_only_this = c("Liver","Retina","Embryo"), size = 14, color = "black") {
      if (is.null(label_only_this)) {
        paste0("<span style='font-size:", size, "pt; color:", color, "'>", x, "</span>")
      } else {
        ifelse(x %in% label_only_this,
               paste0("<span style='font-size:", size, 
                      "pt; color:", color, "'>", x, "</span>"),
               as.character(x))
      }
    }
    
    ggplot(data = data, aes(Name,mean, label = Name,fill=Organ)) +
      geom_bar(position="dodge2", stat="identity",width = 0.85,color="black") +
      # Wrap label in helper function
      facet_nested(.~big_label_md(Organ) + Disease, 
                   scales="free",switch = "x",nest_line = TRUE)+
      theme_classic() +
      theme(legend.text = element_markdown())+
      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.placement = "outside",
        strip.text.x = element_markdown())
0

There are 0 answers