Home > database >  How to Customize Facet_Grid Font Label in R
How to Customize Facet_Grid Font Label in R

Time:11-16

I have this this ggplot with facet_grid function:

set.seed(1)
df <- data.frame(xx = 1:10, x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10), x4 = rnorm(10), x5 = rnorm(10), x6 = rnorm(10), x7 = rnorm(10), x8 = rnorm(10), x9 = rnorm(10), x10 = rnorm(10), x11 = rnorm(10), x12 = rnorm(10))    
library(dplyr)
library(tidyr)
library(ggplot2)

df %>% 
  pivot_longer(-xx) %>% 
  mutate(id = as.numeric(gsub("x", "", name))) %>% 
  arrange(id, xx) %>% 
  select(-id) %>% 
  mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
         phi = rep(rep(list(c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6)), each = 10), 4)) %>% 
  mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
         phi = factor(phi, levels = phi, labels = gsub("c", "", paste("\U03D5 =", phi)))) %>%   
  ggplot(aes(x = xx, y = value))  
  geom_line()   
  geom_point()  
  scale_y_continuous(expand = c(0.0, 0.00))  
  labs(x = "Time", y = "Series")  
  facet_grid(sd ~ phi, scales = "free_y")   
  theme_bw()

provided as a satisfactory answer for this question.

What I want

I wantto increase(or customize) the labels sd = c(sd = 1, sd = 3, sd = 5, sd = 10) at the right side and the labels at the top phi = c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6))of the plot. And also, how to make them bold.

CodePudding user response:

Easiest way is with:

    previous_code_blocks 
    theme(strip.text.x = element_text(size = 10, face = "bold"),
          strip.text.y = element_text(size = 10, face = "bold"))

You can customise with the usual element_text() args here, changing the font family and other such things, hjust, vjust etc.

If you're looking for more control of the labeller in the future, or for more advanced customisations, you can find documentation at: https://ggplot2.tidyverse.org/reference/labeller.html

  • Related