Search code examples
rggplot2ggtext

ggplot2: italics in the legend


I'm trying to edit the labels in the legend so that the first label (WT) is in plain text, whilst the subsequent 7 are in italics. I have been using element_text(face=c("plain", rep("italic",7))) , but this has resulted in none of the labels being converted to italics. I'm a bit confused why it hasn't worked because element_text(face="italic") converts all the labels into italics. My plot is as follows:

library(tidyverse)

for_plot <- read_csv(file =
"Row, Time, Mutant, Mean
1, 1, ppi1, 0.8008
2, 1, sp1-1, 0.8038
3, 1, sp1-1 ppi1, 0.8094
4, 1, sp1-2 ppi1, 0.8138
5, 1, sp1-3 ppi1, 0.8066667
6, 1, sp1-4 ppi1, 0.7998
7, 1, sp1-5 ppi1, 0.8026667
8, 1, Wt, 0.8083333
9,  21, ppi1, 0.6806
10, 21, sp1-1, 0.7088
11, 21, sp1-1 ppi1, 0.6982
12, 21, sp1-2 ppi1, 0.7126
13, 21, sp1-3 ppi1, 0.709
14, 21, sp1-4 ppi1, 0.6942
15, 21, sp1-5 ppi1, 0.7096667
16, 21, Wt, 0.7246667
17, 56, ppi1, 0.6652
18, 56, sp1-1, 0.6848
19, 56, sp1-1 ppi1, 0.6816
20, 56, sp1-2 ppi1, 0.6926
21, 56, sp1-3 ppi1, 0.6945
22, 56, sp1-4 ppi1, 0.676
23, 56, sp1-5 ppi1, 0.6931667
24, 56, Wt, 0.6946667
25, 111, ppi1, 0.653
26, 111, sp1-1, 0.6704
27, 111, sp1-1 ppi1, 0.6704
28, 111, sp1-2 ppi1, 0.6756
29, 111, sp1-3 ppi1, 0.679
30, 111, sp1-4 ppi1, 0.664
31, 111, sp1-5 ppi1, 0.6805
32, 111, Wt, 0.677
33, 186, ppi1, 0.6132
34, 186, sp1-1, 0.633
35, 186, sp1-1 ppi1, 0.6298
36, 186, sp1-2 ppi1, 0.6402
37, 186, sp1-3 ppi1, 0.6435
38, 186, sp1-4 ppi1, 0.6278
39, 186, sp1-5 ppi1, 0.6478333
40, 186, Wt, 0.6403333
41, 281, ppi1, 0.5636
42, 281, sp1-1, 0.587
43, 281, sp1-1 ppi1, 0.5828
44, 281, sp1-2 ppi1, 0.5906
45, 281, sp1-3 ppi1, 0.5968333
46, 281, sp1-4 ppi1, 0.5838
47, 281, sp1-5 ppi1, 0.5983333
48, 281, Wt, 0.5948333")

for_plot %>%
 ggplot()+
  geom_line(aes(x = Time, y = Mean, col = Mutant)) +
  geom_point(aes(x = Time, y = Mean, col = Mutant)) + 
  labs(title = "Effective PSII Quantum Yield across SP1 Mutants", 
       y = "Effective PSII Quantum Yield",
       x = "Actinic PPFD") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.text = element_text(face=c("plain", rep("italic",7))))

Created on 2020-01-01 by the reprex package (v0.3.0)


Solution

  • I wrote the ggtext package to make these types of problems easier to solve. It allows you to style with markdown, i.e., just enclose the italics parts in stars.

    The package is currently under development and needs to be installed via remotes, but it will be released on CRAN soon (spring 2020). You may also need the latest development version of ggplot2, which you can install via remotes::install_github("tidyverse/ggplot2").

    library(tidyverse)
    library(ggtext)  # remotes::install_github("clauswilke/ggtext")
    
    for_plot <- read_csv(file =
    "Row, Time, Mutant, Mean
    1, 1, ppi1, 0.8008
    2, 1, sp1-1, 0.8038
    3, 1, sp1-1 ppi1, 0.8094
    4, 1, sp1-2 ppi1, 0.8138
    5, 1, sp1-3 ppi1, 0.8066667
    6, 1, sp1-4 ppi1, 0.7998
    7, 1, sp1-5 ppi1, 0.8026667
    8, 1, Wt, 0.8083333
    9,  21, ppi1, 0.6806
    10, 21, sp1-1, 0.7088
    11, 21, sp1-1 ppi1, 0.6982
    12, 21, sp1-2 ppi1, 0.7126
    13, 21, sp1-3 ppi1, 0.709
    14, 21, sp1-4 ppi1, 0.6942
    15, 21, sp1-5 ppi1, 0.7096667
    16, 21, Wt, 0.7246667
    17, 56, ppi1, 0.6652
    18, 56, sp1-1, 0.6848
    19, 56, sp1-1 ppi1, 0.6816
    20, 56, sp1-2 ppi1, 0.6926
    21, 56, sp1-3 ppi1, 0.6945
    22, 56, sp1-4 ppi1, 0.676
    23, 56, sp1-5 ppi1, 0.6931667
    24, 56, Wt, 0.6946667
    25, 111, ppi1, 0.653
    26, 111, sp1-1, 0.6704
    27, 111, sp1-1 ppi1, 0.6704
    28, 111, sp1-2 ppi1, 0.6756
    29, 111, sp1-3 ppi1, 0.679
    30, 111, sp1-4 ppi1, 0.664
    31, 111, sp1-5 ppi1, 0.6805
    32, 111, Wt, 0.677
    33, 186, ppi1, 0.6132
    34, 186, sp1-1, 0.633
    35, 186, sp1-1 ppi1, 0.6298
    36, 186, sp1-2 ppi1, 0.6402
    37, 186, sp1-3 ppi1, 0.6435
    38, 186, sp1-4 ppi1, 0.6278
    39, 186, sp1-5 ppi1, 0.6478333
    40, 186, Wt, 0.6403333
    41, 281, ppi1, 0.5636
    42, 281, sp1-1, 0.587
    43, 281, sp1-1 ppi1, 0.5828
    44, 281, sp1-2 ppi1, 0.5906
    45, 281, sp1-3 ppi1, 0.5968333
    46, 281, sp1-4 ppi1, 0.5838
    47, 281, sp1-5 ppi1, 0.5983333
    48, 281, Wt, 0.5948333")
    
    for_plot %>%
     ggplot()+
      geom_line(aes(x = Time, y = Mean, col = Mutant)) +
      geom_point(aes(x = Time, y = Mean, col = Mutant)) + 
      labs(title = "Effective PSII Quantum Yield across SP1 Mutants", 
           y = "Effective PSII Quantum Yield",
           x = "Actinic PPFD") +
      scale_color_hue(
        breaks = c("ppi1", "sp1-1", "sp1-1 ppi1", "sp1-2 ppi1",
                   "sp1-3 ppi1", "sp1-4 ppi1", "sp1-5 ppi1", "Wt"),
        labels = c("ppi1", "*sp1-1*", "*sp1-1 ppi1*", "*sp1-2 ppi1*",
                   "*sp1-3 ppi1*", "*sp1-4 ppi1*", "*sp1-5 ppi1*", "*Wt*")
      ) +
      theme(plot.title = element_text(hjust = 0.5)) +
      theme(legend.text = element_markdown())
    

    This works also for parts of the text, e.g., if you wanted SP1 to be in italics in the title, you could do it like so.

    
    for_plot %>%
      ggplot()+
      geom_line(aes(x = Time, y = Mean, col = Mutant)) +
      geom_point(aes(x = Time, y = Mean, col = Mutant)) + 
      labs(title = "Effective PSII Quantum Yield across *SP1* Mutants", 
           y = "Effective PSII Quantum Yield",
           x = "Actinic PPFD") +
      scale_color_hue(
        breaks = c("ppi1", "sp1-1", "sp1-1 ppi1", "sp1-2 ppi1",
                   "sp1-3 ppi1", "sp1-4 ppi1", "sp1-5 ppi1", "Wt"),
        labels = c("ppi1", "*sp1-1*", "*sp1-1 ppi1*", "*sp1-2 ppi1*",
                   "*sp1-3 ppi1*", "*sp1-4 ppi1*", "*sp1-5 ppi1*", "*Wt*")
      ) +
      theme(plot.title = element_markdown(hjust = 0.5)) +
      theme(legend.text = element_markdown())