Hot questions for Using Ggplot2 in ggtext

Question:

I am close to achieving this multipanel plot with a spanning textgrob figure legend below. But I continue to get an unexpected amount of space between the figures and the figure legend. Attempt in the reprex below.

# Library calls
library(tidyverse)
library(grid)
library(gridtext)
library(ggtext)
library(patchwork)

# make dummy figures
d1 <- runif(500)
d2 <- rep(c("Treatment", "Control"), each=250)
d3 <- rbeta(500, shape1=100, shape2=3)
d4 <- d3 + rnorm(500, mean=0, sd=0.1)
plotData <- data.frame(d1, d2, d3, d4)
str(plotData)
#> 'data.frame':    500 obs. of  4 variables:
#>  $ d1: num  0.0177 0.2228 0.5643 0.4036 0.329 ...
#>  $ d2: Factor w/ 2 levels "Control","Treatment": 2 2 2 2 2 2 2 2 2 2 ...
#>  $ d3: num  0.986 0.965 0.983 0.979 0.99 ...
#>  $ d4: num  0.876 0.816 1.066 0.95 0.982 ...

p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4)) +
  theme(plot.background = element_rect(color='black'))
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2, y=d1, fill=d2))+
  theme(legend.position="none") +
  theme(plot.background = element_rect(color='black'))
p3 <- ggplot(data=plotData) +
  geom_histogram(aes(x=d1, color=I("black"), fill=I("orchid"))) +
  theme(plot.background = element_rect(color='black'))
p4 <- ggplot(data=plotData) +
  geom_histogram(aes(x=d3, color=I("black"), fill=I("goldenrod"))) +
  theme(plot.background = element_rect(color='black'))


fig_legend <- textbox_grob(
  "**Figure 1.**  Testing Control vs. Treatment.   A. Scatterplot. 
  B. The outcomes in the control arm were significantly better than 
  the Treatment Arm. C. Histogram. D. Another Histogram.",
  gp = gpar(fontsize = 11),
  box_gp = gpar(col = "black",   linetype = 1),
  padding = unit(c(3, 3, 3, 3), "pt"),
  margin = unit(c(0,0,0,0), "pt"),
  height = unit(0.6, "in"),
  width = unit(1, "npc"),
  #x = unit(0.5, "npc"), y = unit(0.7, "npc"),
  r = unit(0, "pt")
)


p1 + {
  p2 + {
    p3 +
      p4 +
      plot_layout(ncol=1)
  }
} + fig_legend +
  plot_layout(ncol=1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

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


Answer:

The correct approach is to use plot_annotation(). The reason why there's a little horizontal gap on either side of the caption is that the plot margins are still applied to the caption, just like in regular ggplot2. If you want to avoid that, you have to set the plot margins to 0 and create spacing by adding appropriate margins to the axis titles etc.

# Library calls
library(tidyverse)
library(ggtext)
library(patchwork)

# make dummy figures
d1 <- runif(500)
d2 <- rep(c("Treatment", "Control"), each=250)
d3 <- rbeta(500, shape1=100, shape2=3)
d4 <- d3 + rnorm(500, mean=0, sd=0.1)
plotData <- data.frame(d1, d2, d3, d4)

p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4)) +
  theme(plot.background = element_rect(color='black'))
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2, y=d1, fill=d2))+
  theme(legend.position="none") +
  theme(plot.background = element_rect(color='black'))
p3 <- ggplot(data=plotData) +
  geom_histogram(aes(x=d1, color=I("black"), fill=I("orchid"))) +
  theme(plot.background = element_rect(color='black'))
p4 <- ggplot(data=plotData) +
  geom_histogram(aes(x=d3, color=I("black"), fill=I("goldenrod"))) +
  theme(plot.background = element_rect(color='black'))


fig_legend <- plot_annotation(
  caption = "**Figure 1.**  Testing Control vs. Treatment.   A. Scatterplot. 
  B. The outcomes in the control arm were significantly better than 
  the Treatment Arm. C. Histogram. D. Another Histogram.",
  theme = theme(
    plot.caption = element_textbox_simple(
      size = 11,
      box.colour = "black",
      linetype = 1,
      padding = unit(c(3, 3, 3, 3), "pt"),
      r = unit(0, "pt")
    )
  )
)


p1 + {
  p2 + {
    p3 +
      p4 +
      plot_layout(ncol=1)
  }
} + fig_legend +
  plot_layout(ncol=1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

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

Actually, you can use negative margins on the caption to counteract the plot margins.

fig_legend <- plot_annotation(
  caption = "**Figure 1.**  Testing Control vs. Treatment.   A. Scatterplot. 
  B. The outcomes in the control arm were significantly better than 
  the Treatment Arm. C. Histogram. D. Another Histogram.",
  theme = theme(
    plot.caption = element_textbox_simple(
      size = 11,
      box.colour = "black",
      linetype = 1,
      padding = unit(c(3, 3, 3, 3), "pt"),
      margin = unit(c(0, -5.5, 0, -5.5), "pt"),
      r = unit(0, "pt")
    )
  )
)


p1 + {
  p2 + {
    p3 +
      p4 +
      plot_layout(ncol=1)
  }
} + fig_legend +
  plot_layout(ncol=1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

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

Question:

Consider the following graph:

require(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
  labs(title = 'Iris[small font]' ) +
  theme_classic()

The Left graph is the code output, the right graph shows the desired result, I used Adobe Illustrator for that

The question is, if it is possible to change the font size in line, in this example the "[small font]" label in the title, but of course it is a general question also regarding other labels such as for the axes and legend etc.

Obviously, the font size is set with theme(). However, there might be a way setting a "relative font size", e.g. using rel() and using this somehow with a labeller function??


Answer:

library(grid)
library(gridtext) # devtools::install_github("clauswilke/gridtext")
library(gridExtra)
library(ggplot2)

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() +
  labs(title = "Replace-able") -> gg

gb <- ggplot_build(gg)
gt <- ggplot_gtable(gb)

title <- "<span style='font-size:20'>Iris </span><span style='font-size:12'>[some text]</span>"
tg <- rich_text_grob(title, x = unit(0, "lines"), y = unit(2, "lines"))

gt$grobs[[16]] <- tg

grid.newpage()
grid.draw(gt)

Then, there's:

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() -> gg

title <- "<span style='font-size:20'>Iris </span><span style='font-size:12'>[some text]</span>"
tg <- rich_text_grob(title, x = unit(2, "lines"), y = unit(2, "lines"))

grid.newpage()
grid.draw(
  arrangeGrob(tg, gg, heights=c(0.1, 0.8))
)

Question:

When using unicode characters with the new ggtext package, which expands text formatting options, I get the wrong characters in the title and legend labels.

note: requires ggtext from github (not on CRAN yet). to install, run: devtools::install_github("clauswilke/ggtext")

This issue appears to be a system issue on my computer, as the package developer (Claus Wilke) could not reproduce the same issue on his.

If someone can guide me- why would it be printing the wrong symbol in the plot (titles and legend labels)? also does the same thing if using "&mu;" instead of the "\u03bc" syntax.

Here is system info:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base   
library(ggplot2)
library(ggtext)

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(
    name = NULL,
    values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
    labels = c(
      setosa = "<i style='color:#0072B2'>I. setosa  \u03bc </i>",
      virginica = "<i style='color:#009E73'>I. virginica  \u03bc </i>",
      versicolor = "<i style='color:#D55E00'>I. versicolor  \u03bc </i>")
  ) +
  labs(
    title = "**Fisher's *Iris* dataset  (test unicode symbol: \u03bc)**  
    <span style='font-size:11'>Sepal width vs. sepal length for three *Iris*
    species  \u03bc </span>",
    x = "Sepal length (cm)\n (test unicode symbol: \u03bc)", 
    y = "Sepal width (cm)\n (test unicode symbol: \u03bc)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_markdown(lineheight = 1.1),
    legend.text = element_markdown(size = 11)
  )

Created on 2019-08-09 by the reprex package (v0.3.0)


Answer:

I cannot reproduce the issue, but the following reprex should help pinpoint the source of the problem. It mimics the processing pipeline that is executed inside ggtext.

library(grid)

x <- c(.2, .4, .6, .8)
y <- c(.8, .6, .4, .2)

# original input
text <- c("special char:  \u03bc ")

# convert markdown to html
text2 <- markdown::markdownToHTML(text = text, options = c("use_xhtml", "fragment_only"))

# parse html
doctree <- xml2::read_html(text2)
text3 <- xml2::as_list(doctree)$html$body$p[[1]]

# break text nodes into individual words that get rendered
text4 <- stringr::str_split(stringr::str_squish(text3), "[[:space:]]+")[[1]][3]

# the final rendering is done via grid::textGrob
grid.newpage()
grid.draw(textGrob(c(text, text2, text3, text4), x, y))

Created on 2019-08-10 by the reprex package (v0.3.0)

Question:

I am having trouble adding R2 annotations to a faceted plot, where my R2 values are sometimes <0.01 (yes, it's not a good regression). I would like the 2 of R2 to be superscript. I have tried several options but seem to be stymied by the < symbol in my values

eg, using the iris data set, I first set up a new data frame with my R2 values previously calculated. The x & y positions are also set up as these are different for every facet (not essential for the iris dataset, but it is for mine)

SEr2s <- data.frame(Species = c("virginica",  "setosa",  "versicolor" ), 
                  xpos = c(5,7,7), ypos = c(4.2, 2, 4.2), 
                  lab = c("<0.01","0.08", "0.05"))

Then I run my plot:

XYPlot<-ggplot(data = iris, aes(x=Sepal.Length)) + 
  geom_point(aes(y = Sepal.Width), colour="grey40")+
  geom_smooth(aes(y = Sepal.Width), col="grey40", lwd=0.5, method="lm", se=FALSE) +
  geom_text(data = SEr2s, size=3, vjust=0, hjust=0,
            aes(x = xpos,  y = ypos, 
             label = paste("r^2==",lab)), parse = TRUE)+
  theme_bw() +
  theme(strip.background = element_blank(), strip.placement = "outside",
        panel.grid.minor = element_blank(), legend.position = "right") +
  facet_wrap(~Species)

I get this error:

Error in parse(text = text[[i]]) : :1:7: unexpected '<' 1: r^2== < ^

Is there a way to change my code or my labelling dataframe so that it doesn't try to evaluate these symbols?


Answer:

I think that the easier solution is to define the subscript in your data.frame (SEr2s):

SEr2s <- data.frame(Species = c("virginica",  "setosa",  "versicolor" ), 
                    xpos = c(5,7,7), ypos = c(4.2, 2, 4.2), 
                    lab = c("atop(R^2<0.01)","atop(R^2==0.08)", "atop(R^2==0.05)"))

Then, you can call ggplot without just with label=lab:

ggplot(data = iris, aes(x=Sepal.Length)) + 
    geom_point(aes(y = Sepal.Width), colour="grey40")+
    geom_smooth(aes(y = Sepal.Width), col="grey40", lwd=0.5, method="lm", se=FALSE) +
    geom_text(data = SEr2s, size=3, vjust=0, hjust=0,
              aes(x = xpos,  y = ypos, 
                  label = lab), parse = TRUE)+
    theme_bw() +
    theme(strip.background = element_blank(), strip.placement = "outside",
          panel.grid.minor = element_blank(), legend.position = "right") +
    facet_wrap(~Species)

I think this gives you the plot you want: https://ibb.co/vwbvqp2

Question:

A Data sample has been provided, please scroll to the bottom

I have followed these instructions but I cannot get it to function with my script. They seem to be working with labs() and not the approach I have used below.

I have produced this plot:

However, I want different font sizes for different portions of the text. I have edited the following in Photoshop depicting the result I am seeking.

I have used the following script:

ggplot(as.data.frame(out), aes(x = n.fjernet)) + theme +
  geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .1) +
  geom_line(aes(y = yhat, col = model),size=1) +

  ggtitle("Lymph node yield") + 

  geom_segment(aes(x = 0, y = 1, xend = 100, yend = 1), lty="dashed", size=0.5) +
  geom_segment(aes(x = 25, y = 1, xend = 25, yend = 0.5), lty="dashed", size=0.5, col="black") +

  geom_point(mapping = aes(x = 25, y = 1), size=2, shape=16, col="black", alpha=0.5) +

  scale_fill_manual(values = c("#DAE5F2", "#F9E7E5","#E4F2F3","#FAF1D9"), name = "",
                    labels = c("Overall survival\nNot adjusted to metastatic burden", "Event-free survival\nNot adjusted to metastatic burden","Overall survival\nAdjusted to metastatic burden", "Event-free survival\nAdjusted to metastatic burden")) +
  scale_colour_manual(values = c("#2C77BF", "#E38072","#6DBCC3","#E1B930"), name = "",
                      labels = c("Overall survival\nNot adjusted to metastatic burden", "Event-free survival\nNot adjusted to metastatic burden","Overall survival\nAdjusted to metastatic burden", "Event-free survival\nAdjusted to metastatic burden")) +

  scale_x_continuous(name="", breaks=seq(0,100,by=25), limits=c(0,100), label=c("0","25\nas reference","50", "75", "100")) +
  scale_y_continuous(name="Hazard ratio", breaks = seq(0.5,1.2,by=.1)) +coord_cartesian(ylim=c(0.5,1.25)) +

  theme(axis.text.x = element_text(color = "grey20", size =11), 
        axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
        axis.text.y = element_text(color = "grey20", size = 11), 
        axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
        legend.key = element_rect(fill = "white"),
        plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5),
        legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top") +
        guides(fill=guide_legend(nrow=2,byrow=TRUE))

A data sample

out <- structure(list(model = c("1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", 
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
"4", "4", "4", "4"), n.fjernet = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 0, 1, 2, 3, 
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 
98, 99, 100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46
), yhat = c(1.06995503357619, 1.06706507368365, 1.06418291960344, 
1.06130855025195, 1.05844194460257, 1.05558308168545, 1.05273194058739, 
1.04988850045166, 1.0470527404779, 1.0442246399219, 1.0414041780955, 
1.0385913343664, 1.03578608815804, 1.03298841894944, 1.03019830627504, 
1.02741572972456, 1.02464066894286, 1.02187310362975, 1.01911301353991, 
1.01636037848267, 1.01361517832191, 1.0108773929759, 1.00814700241714, 
1.00542398667224, 1.00270832582174, 1, 0.997298989395025, 0.994605274248338, 
0.991918834854829, 0.989239651562611, 0.986567704772878, 0.983902974939761, 
0.981245442570182, 0.978595088223716, 0.975951892512447, 0.973315836100825, 
0.970686899705527, 0.968065064095311, 0.965450310090884, 0.962842618564752, 
0.960241970441086, 0.957648346695583, 0.955061728355321, 0.952482096498627, 
0.949909432254935, 0.947343716804649, 0.944784931379003, 0.942233057259927, 
0.93968807577991, 0.937149968321859, 0.93461871631897, 0.932094301254584, 
0.929576704662058, 0.927065908124628, 0.924561893275272, 0.92206464179658, 
0.919574135420615, 0.917090355928783, 0.914613285151699, 0.912142904969053, 
0.909679197309478, 0.90722214415042, 0.904771727518001, 0.902327929486893, 
0.899890732180184, 0.897460117769246, 0.895036068473609, 0.892618566560827, 
0.890207594346348, 0.887803134193389, 0.885405168512802, 0.88301367976295, 
0.880628650449572, 0.878250063125662, 0.87587790039134, 0.873512144893719, 
0.871152779326786, 0.868799786431271, 0.86645314899452, 0.864112849850372, 
0.86177887187903, 0.859451198006942, 0.857129811206666, 0.854814694496757, 
0.852505830941632, 0.850203203651456, 0.847906795782009, 0.845616590534571, 
0.843332571155794, 0.841054720937581, 0.838783023216965, 0.836517461375982, 
0.834258018841559, 0.832004679085382, 0.829757425623784, 0.827516242017617, 
0.825281111872138, 0.823052018836886, 0.820828946605561, 0.818611878915908, 
0.816400799549598, 1.09390435986962, 1.08998414105606, 1.08607797110829, 
1.08218579967948, 1.07830757660322, 1.07444325189287, 1.07059277574093, 
1.06675609851842, 1.06293317077417, 1.05912394323428, 1.05532836680139, 
1.0515463925541, 1.04777797174635, 1.04402305580675, 1.04028159633797, 
1.03655354511615, 1.03283885409023, 1.02913747538134, 1.02544936128221, 
1.02177446425654, 1.01811273693838, 1.01446413213151, 1.01082860280888, 
1.00720610211193, 1.00359658335007, 1, 0.996416305705164, 0.992845454275126, 
0.989287399684986, 0.985742096074781, 0.982209497748898, 0.978689559175481, 
0.975182234985848, 0.971687479973904, 0.968205249095557, 0.964735497468143, 
0.96127818036984, 0.957833253239098, 0.95440067167406, 0.950980391431994, 
0.947572368428718, 0.944176558738035, 0.940792918591168, 0.93742140437619, 
0.93406197263747, 0.930714580075105, 0.927379183544369, 0.924055740055151, 
0.920744206771404, 0.917444541010594, 0.914156700243146, 0.910880642091898, 
0.907616324331556, 0.904363704888149, 0.901122741838484, 0.89789339340961, 
0.894675617978277, 0.891469374070399, 0.888274620360521, 0.885091315671288, 
0.881919418972907, 0.878758889382628, 0.875609686164211, 0.872471768727401, 
0.869345096627407, 0.866229629564379, 0.863125327382891, 0.86003215007142, 
0.856950057761833, 0.853879010728873, 0.850818969389643, 0.847769894303103, 
0.844731746169555, 0.84170448583014, 0.838688074266332, 0.835682472599437, 
0.832687642090087, 0.829703544137748, 0.826730140280216, 0.823767392193125, 
0.82081526168945, 0.817873710719019, 0.814942701368018, 0.812022195858507, 
0.809112156547929, 0.806212545928625, 0.803323326627355, 0.800444461404812, 
0.797575913155142, 0.794717644905469, 0.791869619815415, 0.789031801176629, 
0.786204152412307, 0.783386637076731, 0.780579218854788, 0.77778186156151, 
0.774994529141605, 0.772217185668991, 0.769449795346334, 0.766692322504588, 
0.763944731602534, 1.11341569090863, 1.10864127602262, 1.10388733420672, 
1.09915377767079, 1.09444051900111, 1.08974747115884, 1.08507454747834, 
1.0804216616656, 1.07578872779666, 1.07117566031599, 1.06658237403496, 
1.06200878413019, 1.05745480614207, 1.05292035597315, 1.04840534988657, 
1.04390970450458, 1.03943333680694, 1.03497616412939, 1.03053810416218, 
1.02611907494849, 1.02171899488293, 1.01733778271005, 1.01297535752282, 
1.00863163876117, 1.00430654621045, 1, 0.995711920601629, 0.991442228828184, 
0.987190845832071, 0.982957693103798, 0.978742692470529, 0.97454576609464, 
0.97036683647228, 0.96620582643194, 0.962062659133031, 0.957937258064461, 
0.953829547043222, 0.949739450212989, 0.94566689204271, 0.94161179732522, 
0.937574091175846, 0.933553699031029, 0.929550546646941, 0.925564560098119, 
0.9215956657761, 0.917643790388057, 0.913708860955451, 0.909790804812679, 
0.905889549605734, 0.90200502329087, 0.898137154133269, 0.894285870705718, 
0.890451101887291, 0.886632776862031, 0.882830825117648, 0.879045176444214, 
0.875275760932866, 0.871522508974516, 0.867785351258566, 0.864064218771626, 
0.860359042796242, 0.856669754909625, 0.852996286982389, 0.849338571177293, 
0.845696539947986, 0.842070126037761, 0.838459262478315, 0.834863882588508, 
0.831283919973136, 0.827719308521702, 0.824169982407196, 0.82063587608488, 
0.817116924291076, 0.813613062041963, 0.810124224632375, 0.806650347634607, 
0.803191366897227, 0.799747218543885, 0.796317838972142, 0.792903164852291, 
0.789503133126184, 0.786117681006076, 0.782746745973459, 0.779390265777908, 
0.776048178435935, 0.77272042222984, 0.769406935706576, 0.766107657676608, 
0.762822527212791, 0.759551483649236, 0.756294466580198, 0.753051415858953, 
0.749822271596694, 0.74660697416142, 0.743405464176839, 0.740217682521265, 
0.737043570326536, 0.733883068976917, 0.730736120108023, 0.727602665605742, 
0.724482647605159, 1.1320553450125, 1.1264526815186, 1.12087774620809, 
1.11533040185123, 1.10981051189742, 1.10431794047186, 1.09885255237224, 
1.09341421306535, 1.0880027886838, 1.08261814602275, 1.07726015253658, 
1.07192867633566, 1.0666235861831, 1.06134475149152, 1.05609204231981, 
1.05086532936997, 1.04566448398391, 1.04048937814028, 1.03533988445131, 
1.03021587615971, 1.0251172271355, 1.02004381187294, 1.01499550548744, 
1.00997218371247, 1.00497372289651, 1, 0.99505089259232, 0.990126278848773, 
0.985226037547583, 0.980350048066917, 0.97549819038191, 0.970670345061712, 
0.965866393266552, 0.961086216744807, 0.956329697830096, 0.951596719438381, 
0.946887165065084, 0.942200918782223, 0.937537865235555, 0.932897889641737, 
0.928280877785502, 0.923686716016846, 0.919115291248231, 0.914566490951803, 
0.910040203156617, 0.905536316445888, 0.901054719954242), lower = c(0.931198841176898, 
0.933857755314683, 0.936524261627286, 0.939198381793148, 0.941880137552615, 
0.944569550708104, 0.947266643124289, 0.949971436728276, 0.952683953509779, 
0.955404215521302, 0.958132244878317, 0.960868063759443, 0.963611694406628, 
0.966363159125328, 0.96912248028469, 0.971889680317732, 0.974664781721529, 
0.977447807057391, 0.980238778951049, 0.983037720092839, 0.985844653237888, 
0.988659601206295, 0.99148258688332, 0.994313633219567, 0.997152763231174, 
1, 0.99177339754103, 0.983614472070078, 0.975522666835468, 0.967497429665698, 
0.959538212931763, 0.951644473509783, 0.943815672743943, 0.936051276409733, 
0.928350754677499, 0.920713582076282, 0.913139237457967, 0.905627203961713, 
0.898176968978691, 0.890788024117101, 0.883459865167478, 0.87619199206829, 
0.868983908871812, 0.861835123710281, 0.85474514876234, 0.847713500219739, 
0.840739698254329, 0.833823266985317, 0.826963734446789, 0.82016063255551, 
0.813413497078978, 0.806721867603749, 0.800085287504015, 0.793503303910449, 
0.786975467679298, 0.780501333361739, 0.774080459173476, 0.767712406964599, 
0.761396742189682, 0.755133033878133, 0.748920854604782, 0.742759780460716, 
0.736649391024354, 0.730589269332754, 0.724579001853164, 0.718618178454801, 
0.712706392380864, 0.70684324022078, 0.701028321882674, 0.695261240566066, 
0.689541602734799, 0.683869018090179, 0.678243099544345, 0.672663463193854, 
0.667129728293484, 0.661641517230253, 0.65619845549765, 0.650800171670081, 
0.645446297377522, 0.640136467280383, 0.634870319044577, 0.629647493316798, 
0.624467633699994, 0.619330386729051, 0.614235401846671, 0.609182331379452, 
0.604170830514165, 0.599200557274219, 0.594271172496331, 0.589382339807378, 
0.584533725601445, 0.579724999017061, 0.574955831914621, 0.570225898853993, 
0.565534877072313, 0.560882446461956, 0.556268289548699, 0.551692091470051, 
0.547153539953769, 0.542652325296551, 0.538188140342901, 0.961892022918095, 
0.963388083737925, 0.964886471428051, 0.966387189607529, 0.967890241901041, 
0.969395631938907, 0.970903363357095, 0.972413439797228, 0.97392586490659, 
0.975440642338142, 0.976957775750523, 0.978477268808065, 0.979999125180797, 
0.981523348544458, 0.983049942580502, 0.984578910976112, 0.986110257424203, 
0.987643985623434, 0.989180099278218, 0.990718602098728, 0.992259497800909, 
0.993802790106485, 0.995348482742968, 0.996896579443668, 0.998447083947701, 
1, 0.99130364863173, 0.982682923790581, 0.974137167801699, 0.965665728709605, 
0.957267960228449, 0.948943221692716, 0.940690878008338, 0.932510299604252, 
0.924400862384363, 0.916361947679937, 0.9083929422024, 0.900493237996551, 
0.892662232394182, 0.884899327968098, 0.877203932486542, 0.869575458868011, 
0.86201332513647, 0.854516954376953, 0.847085774691547, 0.839719219155767, 
0.832416725775299, 0.825177737443132, 0.818001701897053, 0.810888071677514, 
0.803836304085867, 0.796845861142965, 0.789916209548114, 0.783046820638392, 
0.776237170348314, 0.769486739169854, 0.762795012112809, 0.756161478665512, 
0.749585632755886, 0.743066972712834, 0.736605001227967, 0.730199225317664, 
0.723849156285463, 0.717554309684779, 0.711314205281944, 0.705128367019571, 
0.698996322980234, 0.692917605350469, 0.686891750385082, 0.680918298371767, 
0.674996793596042, 0.669126784306475, 0.663307822680226, 0.657539464788876, 
0.651821270564568, 0.646152803766427, 0.640533631947281, 0.634963326420674, 
0.629441462228154, 0.623967618106861, 0.618541376457381, 0.613162323311894, 
0.60783004830259, 0.602544144630358, 0.597304209033759, 0.592109841758255, 
0.586960646525714, 0.58185623050418, 0.576796204277899, 0.571780181817614, 
0.566807780451114, 0.561878620834042, 0.556992326920951, 0.552148525936616, 
0.547346848347599, 0.542586927834053, 0.537868401261778, 0.533190908654516, 
0.52855409316649, 0.523957601055177, 0.519401081654325, 0.963917275108263, 
0.965335267842061, 0.966755346546767, 0.968177514290995, 0.969601774147877, 
0.971028129195062, 0.972456582514729, 0.973887137193588, 0.975319796322895, 
0.976754562998448, 0.978191440320602, 0.979630431394273, 0.981071539328944, 
0.982514767238671, 0.983960118242094, 0.985407595462437, 0.986857202027521, 
0.988308941069769, 0.989762815726209, 0.991218829138486, 0.992676984452867, 
0.994137284820245, 0.99559973339615, 0.997064333340755, 0.99853108781888, 
1, 0.989985887261381, 0.980072056976705, 0.970257504906171, 0.96054123686655, 
0.950922268630476, 0.941399625826748, 0.931972343841626, 0.922639467721122, 
0.913400052074264, 0.904253160977332, 0.895197867879053, 0.886233255506742, 
0.877358415773384, 0.868572449685654, 0.859874467252844, 0.851263587396715, 
0.842738937862244, 0.834299655129267, 0.825944884325013, 0.817673779137497, 
0.809485501729802, 0.801379222655203, 0.793354120773147, 0.785409383166078, 
0.777544205057084, 0.769757789728383, 0.762049348440613, 0.754418100352938, 
0.74686327244395, 0.739384099433363, 0.731979823704495, 0.724649695227524, 
0.717392971483511, 0.710208917389182, 0.703096805222475, 0.696055914548815, 
0.689085532148141, 0.682184951942659, 0.675353474925316, 0.668590409088996, 
0.66189506935642, 0.655266777510749, 0.648704862126886, 0.642208658503457, 
0.635777508595487, 0.629410760947734, 0.623107770628704, 0.616867899165319, 
0.610690514478243, 0.604574990817853, 0.598520708700853, 0.592527054847525, 
0.586593422119601, 0.580719209458763, 0.574903821825762, 0.569146670140136, 
0.563447171220544, 0.557804747725686, 0.552218828095824, 0.546688846494885, 
0.54121424275314, 0.535794462310464, 0.53042895616016, 0.525117180793344, 
0.519858598143894, 0.514652675533941, 0.509498885619913, 0.504396706339115, 
0.499345620856847, 0.494345117514051, 0.48939468977548, 0.484493836178387, 
0.479642060281731, 0.474838870615887, 0.470083780632861, 0.991175581844016, 
0.991527058864988, 0.991878660521896, 0.992230386858937, 0.992582237920323, 
0.992934213750281, 0.993286314393057, 0.993638539892908, 0.99399089029411, 
0.994343365640955, 0.994695965977748, 0.995048691348811, 0.995401541798484, 
0.995754517371118, 0.996107618111084, 0.996460844062767, 0.996814195270567, 
0.997167671778901, 0.997521273632202, 0.997875000874918, 0.998228853551512, 
0.998582831706465, 0.99893693538427, 0.999291164629441, 0.999645519486503, 
1, 0.98977529837702, 0.979655141277318, 0.969638459764339, 0.959724195831082, 
0.949911302288355, 0.94019874265416, 0.93058549104422, 0.921070532063618, 
0.911652860699548, 0.902331482215158, 0.893105412044486, 0.883973675688463, 
0.874935308611979, 0.865989356142011, 0.857134873366782, 0.848370925035956, 
0.839696585461851, 0.831110938421669, 0.822613077060712, 0.814202103796604, 
0.805877130224481), upper = c(1.22938702589896, 1.21927334756868, 
1.20924287044941, 1.19929491007788, 1.1894287876216, 1.17964382983259, 
1.16993936900138, 1.1603147429115, 1.15076929479429, 1.14130237328403, 
1.13191333237354, 1.1226015313701, 1.11336633485168, 1.10420711262366, 
1.09512323967574, 1.08611409613934, 1.07717906724532, 1.06831754328197, 
1.05952891955345, 1.0508125963385, 1.04216797884954, 1.03359447719208, 
1.02509150632444, 1.01665848601784, 1.00829484081683, 1, 1.00285536667381, 
1.00571888646646, 1.00859058265809, 1.01147047859533, 1.01435859769145, 
1.01725496342659, 1.02015959934792, 1.02307252906987, 1.02599377627426, 
1.02892336471057, 1.03186131819607, 1.03480766061603, 1.03776241592396, 
1.04072560814172, 1.04369726135978, 1.04667739973741, 1.04966604750285, 
1.05266322895352, 1.05566896845622, 1.05868329044732, 1.06170621943298, 
1.06473777998932, 1.06777799676265, 1.07082689446963, 1.07388449789751, 
1.07695083190433, 1.08002592141908, 1.08310979144195, 1.0862024670445, 
1.08930397336991, 1.09241433563312, 1.09553357912107, 1.09866172919293, 
1.10179881128026, 1.10494485088723, 1.10809987359085, 1.11126390504115, 
1.11443697096141, 1.11761909714835, 1.12081030947236, 1.12401063387769, 
1.12722009638267, 1.13043872307992, 1.13366654013659, 1.13690357379451, 
1.14014985037045, 1.14340539625635, 1.14667023791947, 1.14994440190267, 
1.15322791482459, 1.15652080337989, 1.15982309433943, 1.16313481455051, 
1.16645599093713, 1.16978665050011, 1.17312682031742, 1.1764765275443, 
1.17983579941357, 1.18320466323578, 1.18658314639948, 1.18997127637141, 
1.19336908069675, 1.19677658699932, 1.20019382298183, 1.20362081642608, 
1.20705759519321, 1.21050418722389, 1.21396062053859, 1.21742692323779, 
1.2209031235022, 1.224389249593, 1.22788532985205, 1.23139139270217, 
1.23490746664731, 1.23843358027281, 1.24403438227042, 1.23321582216799, 
1.22249134406551, 1.21186012979284, 1.20132136829497, 1.19087425557006, 
1.1805179946082, 1.17025179533052, 1.16007487452898, 1.14998645580657, 
1.13998576951813, 1.13007205271157, 1.12024454906973, 1.11050250885263, 
1.1008451888403, 1.09127185227608, 1.08178176881038, 1.07237421444502, 
1.06304847147793, 1.05380382844846, 1.04463958008304, 1.03555502724144, 
1.02654947686337, 1.01762224191565, 1.00877264133979, 1, 1.00155533135132, 
1.00311308175825, 1.00467325498323, 1.00623585479454, 1.00780088496633, 
1.0093683492786, 1.01093825151727, 1.0125105954741, 1.01408538494679, 
1.01566262373891, 1.01724231565998, 1.01882446452542, 1.02040907415658, 
1.02199614838079, 1.0235856910313, 1.02517770594732, 1.02677219697406, 
1.02836916796268, 1.02996862277034, 1.03157056526021, 1.03317499930146, 
1.03478192876928, 1.03639135754487, 1.0380032895155, 1.03961772857446, 
1.0412346786211, 1.04285414356084, 1.04447612730518, 1.04610063377168, 
1.04772766688402, 1.04935723057197, 1.05098932877142, 1.05262396542436, 
1.05426114447894, 1.05590086988942, 1.05754314561625, 1.059187975626, 
1.06083536389143, 1.06248531439148, 1.06413783111128, 1.06579291804213, 
1.06745057918158, 1.06911081853336, 1.07077364010747, 1.07243904792009, 
1.0741070459937, 1.07577763835701, 1.077450829045, 1.07912662209892, 
1.08080502156631, 1.08248603150102, 1.08416965596318, 1.08585589901925, 
1.08754476474202, 1.08923625721058, 1.09093038051042, 1.09262713873334, 
1.09432653597751, 1.0960285763475, 1.09773326395424, 1.09944060291505, 
1.10115059735368, 1.10286325140027, 1.10457856919139, 1.10629655487005, 
1.1080172125857, 1.10974054649424, 1.11146656075804, 1.11319525954593, 
1.11492664703324, 1.11666072740179, 1.11839750483991, 1.12013698354243, 
1.12187916771071, 1.12362406155264, 1.28610051170864, 1.27322135619119, 
1.26047117398908, 1.24784867354897, 1.23535257625132, 1.22298161628079, 
1.2107345404981, 1.19861010831301, 1.18660709155872, 1.1747242743674, 
1.1629604530471, 1.15131443595973, 1.13978504340043, 1.12837110747803, 
1.11707147199674, 1.10588499233907, 1.09481053534984, 1.08384697922142, 
1.07299321338009, 1.06224813837353, 1.05161066575947, 1.04107971799542, 
1.03065422832952, 1.0203331406925, 1.01011540959066, 1, 1.00147107305825, 
1.00294431017245, 1.00441971452608, 1.0058972893073, 1.00737703770897, 
1.00885896292865, 1.01034306816859, 1.01182935663577, 1.01331783154186, 
1.0148084961033, 1.0163013535412, 1.01779640708146, 1.01929365995471, 
1.02079311539632, 1.02229477664643, 1.02379864694995, 1.02530472955655, 
1.02681302772071, 1.02832354470165, 1.02983628376343, 1.03135124817489, 
1.03286844120968, 1.03438786614626, 1.03590952626794, 1.03743342486282, 
1.03895956522387, 1.04048795064888, 1.04201858444052, 1.04355146990629, 
1.04508661035857, 1.04662400911462, 1.04816366949655, 1.04970559483138, 
1.05124978845104, 1.05279625369233, 1.05434499389697, 1.05589601241159, 
1.05744931258777, 1.05900489778199, 1.06056277135567, 1.0621229366752, 
1.0636853971119, 1.06525015604205, 1.06681721684691, 1.06838658291269, 
1.06995825763062, 1.07153224439687, 1.07310854661266, 1.07468716768416, 
1.0762681110226, 1.07785138004418, 1.07943697817017, 1.08102490882684, 
1.08261517544551, 1.08420778146257, 1.08580273031943, 1.08740002546258, 
1.08899967034358, 1.09060166841908, 1.09220602315077, 1.09381273800549, 
1.09542181645515, 1.09703326197676, 1.09864707805247, 1.10026326816952, 
1.10188183582031, 1.10350278450237, 1.10512611771836, 1.10675183897611, 
1.1083799517886, 1.11001045967398, 1.11164336615559, 1.11327867476193, 
1.1149163890267, 1.11655651248881, 1.29295891428957, 1.27973879518019, 
1.26665384784412, 1.25370269019031, 1.24088395425919, 1.22819628607814, 
1.21563834551854, 1.20320880615416, 1.19090635512109, 1.17872969297907, 
1.16667753357421, 1.15474860390318, 1.14294164397872, 1.13125540669655, 
1.1196886577037, 1.10824017526804, 1.09690875014933, 1.08569318547141, 
1.07459229659587, 1.06360491099682, 1.05272986813714, 1.04196601934584, 
1.03131222769674, 1.02076736788841, 1.01033032612528, 1, 1.00035460621449, 
1.00070933817455, 1.00106419592476, 1.00141917950974, 1.0017742889741, 
1.00212952436249, 1.00248488571955, 1.00284037308996, 1.0031959865184, 
1.00355172604957, 1.00390759172819, 1.00426358359899, 1.00461970170672, 
1.00497594609614, 1.00533231681204, 1.00568881389921, 1.00604543740246, 
1.00640218736662, 1.00675906383654, 1.00711606685707, 1.00747319647309
)), row.names = c(NA, 350L), class = c("Predict", "data.frame"))

Answer:

You can do this with the ggtext package currently under development.

# this requires the current development versions of ggplot2 and ggtext
# remotes::install_github("tidyverse/ggplot2")
# remotes::install_github("clauswilke/ggtext")

library(tidyverse)
library(ggtext)

df <- tibble(reference = seq(0, 100, by = 5)) %>%
  mutate(
    overall_not_adj = 1 + .08/(-25) * (reference - 25),
    event_free_not_adj = 1 + .1/(-25) * (reference - 25),
    overall_adj = 1 + .12/(-25) * (reference - 25),
    event_free_adj = 1 + .14/(-25) * (reference - 25)
  ) %>%
  pivot_longer(-reference, names_to = "lymph_node_yield", values_to = "hazard_ratio")


ggplot(df, aes(reference, hazard_ratio, color = lymph_node_yield)) +
  geom_line() +
  scale_color_manual(
    name = "Lymph node yield",
    breaks = c(
      "overall_not_adj", "overall_adj", 
      "event_free_not_adj", "event_free_adj"
    ),
    labels = c(
      "Overall survival<br><span style='font-size:7pt'>Not adjusted to metastatic burden</span>",
      "Overall survival<br><span style='font-size:7pt'>Adjusted to metastatic burden</span>",
      "Event-free survival<br><span style='font-size:7pt'>Not adjusted to metastatic burden</span>",
      "Event-free survival<br><span style='font-size:7pt'>Adjusted to metastatic burden</span>"
    ),
    values = c(
      overall_not_adj = "#0072B2", overall_adj = "#009E73",
      event_free_not_adj = "#CC79A7", event_free_adj = "#E69F00"
    ),
    guide = guide_legend(title.position = "top", ncol = 2)
  ) +
  theme_classic() +
  theme(
    legend.position = "top",
    legend.title.align = 0.5,
    legend.text = element_markdown(),
    legend.key.height = grid::unit(20, "pt")
  )

Created on 2019-12-03 by the reprex package (v0.3.0)

Question:

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)


Answer:

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())