Home > Blockchain >  In R/ggplot2 how to combine labeller (renaming plots) and factor (changing order of plots) in a face
In R/ggplot2 how to combine labeller (renaming plots) and factor (changing order of plots) in a face

Time:09-14

I have a ggplot that I use facet_wrap on. I changed the lables of the individual plots with labeller according to this guide:

# New facet label names for dose variable
dose.labs <- c("D0.5", "D1", "D2")
names(dose.labs) <- c("0.5", "1", "2")

# New facet label names for supp variable
supp.labs <- c("Orange Juice", "Vitamin C")
names(supp.labs) <- c("OJ", "VC")

# Create the plot
p   facet_grid(
  dose ~ supp, 
  labeller = labeller(dose = dose.labs, supp = supp.labs)
  )

I now also want to change the order of the plots. For this I referred to this post.

But the solution for the renaming and relabelling provided in the comment by glenn_in_boston didn't work for me. I have tried their solution only, I have tried playing around with the labeller I used before I had the desire to reorder my plots.

I can apply whatever solution someone has with a dummy data frame, no need to use my (very extensive) data frame. But if it helps, this is what I have without reordering the plots:

v1.labs <- c("T/trans", "T/transmann, T/trans-Frau", "T/transgendermann, T/transgender-Frau", "...transsexue...", "...Transsexue...", "transgender", "Transgender", "transident...", "...Transident...",  "...T/transgeschlechtlich...", "...T/transvestit...", "...T/transe(n)", "...T/tranny/ies, ...S/shemale...", "...T/travo...")

names(v1.labs) <- c("d_trans", "d_transX", "d_transgenderX", "d_transsexue", "d_Transsexue", "d_transgender", "d_Transgender", "d_transident", "d_Transident", "d_transgeschlechtlich", "d_transvestit", "d_transe", "d_tranny", "d_travo")

qy %>%
  pivot_longer(c(d_trans, d_transX, d_transgenderX , d_transsexue, d_Transsexue, d_transgender, d_Transgender, d_transident, d_Transident, d_transgeschlechtlich, d_transvestit, d_transe, d_tranny, d_travo), names_to ="words", values_to = "d") %>%
  ggplot(aes(x = year, y = d))  
  geom_line(aes(color = words), show.legend = FALSE)  
  labs(title = "Sprachwandel: Vergleich 2",
       subtitle = "Personenbezeichnungen",
       x = "Jahr",
       y = "Wortdichte pro Artikel in Prozent",
       linetype = "Worte") 
  facet_wrap(~words, labeller = labeller(words = v1.labs))

And what I would like the order to be:

levels = c("d_Transgender", "d_transgender", "d_transgenderX", "d_Transident", "d_Transsexue", "d_transsexue", "d_transX", "d_transident", "d_transvestit", "d_travo", "d_trans", "d_transgeschlechtlich", "d_transe", "d_tranny")

CodePudding user response:

You can do this in ggplot2 using the labeller function as you've specified, by using factor(levels=) to reorder the variable in your desired order:

library(tidyverse)
cyls = c('four','six','eight')
names(cyls) = c('4','6','8')

mtcars %>% 
    mutate(cyl = factor(cyl, levels = c(6,4,8))) %>% 
    ggplot(aes(x = hp, y = mpg))   
    geom_point()   
    facet_wrap(~cyl, labeller = as_labeller(cyls))

Note you can also avoid all the labeller difficulties by just bringing in a properly-labeled version of the variable instead:

newnames <- data.frame(cyl = c(4,6,8), cyl_label = c('four','six','eight'))
mtcars <- mtcars %>% 
    inner_join(newnames, by = 'cyl')

and then facet using cyl_label instead of cyl

  • Related