Search code examples
rggplot2plotpatchwork

Bolden the title and axis-ticks


So I got help to change the scales on my graph for different facets. The code below was suggested but now I am unable to make the fonts of the labels and axis ticks bold. In my old code I used

theme(
     axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),axis.text.y = element_text(size = 12, face = "bold"),
     axis.title.y = element_text(vjust= 1.8, size = 16),
     axis.title.x = element_text(vjust= -0.5, size = 16),
     axis.title = element_text(face = "bold"))

But it is not working.

tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("Visible", "Remembered"), class = "factor"), 
    Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
    ), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L, 
    1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
    ), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "Post-adaptation"
    ), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12, 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12), EE = c(2.134379625, 0.333942625, 1.742841125, 0.317361916666667, 
    2.84197270833333, 0.307057416666667, 2.403112375, 0.281202, 
    3.49590529166667, 0.305657666666667, 2.85211466666667, 0.3131155, 
    1.44857545833333, 0.269328166666667, 1.740270875, 0.243361833333333, 
    2.10702266666667, 0.286209125, 2.145855125, 0.305474083333333, 
    1.60016616666667, 0.281528625, 1.94182179166667, 0.294655916666667
    ), sd = c(0.727246182828044, 0.0816168443914292, 0.549168068103643, 
    0.0894916121701392, 1.14554677132408, 0.0958562360654162, 
    1.06827971273128, 0.0953131237162305, 1.18204258551111, 0.0896670491921828, 
    1.32864473484909, 0.109865886496798, 0.605344957514288, 0.0815454655757737, 
    0.833908172662699, 0.0798994165789182, 1.11582277105041, 
    0.0976064300150272, 0.667812406644538, 0.142929179817685, 
    0.686043669971901, 0.109794818975944, 1.39509308576833, 0.161854932615856
    ), se = c(0.209937889711449, 0.0235607535398997, 0.158531165974993, 
    0.0258340031883217, 0.330690868396632, 0.0276713118479362, 
    0.308385789857611, 0.0275145288174349, 0.341226302469221, 
    0.0258846474942731, 0.383546697661249, 0.0317155495718416, 
    0.174748037086728, 0.0235401482506832, 0.240728553983119, 
    0.0230649748349663, 0.322110288616933, 0.0281765493219072, 
    0.192780836372198, 0.0412601002213964, 0.198043748767058, 
    0.0316950341456936, 0.402728684306467, 0.0467234944577166
    ), ci = c(0.462070179795855, 0.0518568689018959, 0.348924743722983, 
    0.0568602576432562, 0.727845693918804, 0.0609041467375754, 
    0.678752547059741, 0.0605590696140879, 0.751034027967696, 
    0.0569717250090983, 0.844180589754564, 0.069805453951774, 
    0.384617836383033, 0.0518115169661108, 0.529839974927164, 
    0.0507656673296478, 0.708959965158704, 0.0620161669201078, 
    0.424307760005262, 0.0908128682911871, 0.435891352085212, 
    0.0697602998032695, 0.886399857701764, 0.102837717929058)), row.names = c(NA, 
-24L), class = "data.frame")
ls_p <- 
   tgc %>%
   split(., .$Session) %>%
   map(function(x){
     ggplot(x, aes(x = Condition, y = EE, fill = test)) +
       geom_errorbar(aes(ymin = EE - se, ymax = EE + se, group = test), position = position_dodge(0.5), width = .1) +
       geom_col(width = 0.5, color = "black", position = "dodge") +
       labs(x= "Workspace", y = "EE (cm)") +
       scale_fill_manual("Test:", values = c("grey80", "grey20")) +
       facet_nested(. ~ Session + Group, scales = "free_y") +
       cowplot::theme_half_open() 
   })
 
 wrap_plots(ls_p) + plot_layout(guides = "collect") & 
   theme(legend.position = "bottom", legend.margin=margin(t=0, r=3, b=5, l=3, unit="cm")) + theme(aspect.ratio = 3/2) 
 

Solution

  • You have two options when calling theme with patchwork. You can either do this at a plot level (using +), or at the patchwork level (using &). Below just one way that is using both options, for demonstration purpose.

    Why you should call theme() only once?

    It's not technically wrong to call theme several times, but it clutters the code. Why is that bad? You're prone to create bugs/errors, as it is easy to create contradicting theme calls.

    library(tidyverse)
    library(ggh4x)
    library(patchwork)
    
    ls_p <- 
      tgc %>%
      split(., .$Session) %>%
      map(function(x){
        ggplot(x, aes(x = Condition, y = EE, fill = test)) +
          geom_errorbar(aes(ymin = EE - se, ymax = EE + se, group = test), position = position_dodge(0.5), width = .1) +
          geom_col(width = 0.5, color = "black", position = "dodge") +
          labs(x= "Workspace", y = "EE (cm)") +
          scale_fill_manual("Test:", values = c("grey80", "grey20")) +
          facet_nested(. ~ Session + Group, scales = "free_y") +
          cowplot::theme_half_open() +
          theme(axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),axis.text.y = element_text(size = 12, face = "bold"),
                axis.title.y = element_text(vjust= 1.8, size = 16),
                axis.title.x = element_text(vjust= -0.5, size = 16),
                axis.title = element_text(face = "bold"))
      })
    
    wrap_plots(ls_p) + plot_layout(guides = "collect") & 
      theme(legend.position = "bottom",
            aspect.ratio = 3/2)