r ggplot2:图例中的不同字体大小

r ggplot2: varying font sizes in legend

我的传说中有两行。 我怎样才能使一行粗体、蓝色和大字体和一行小字体、红色和斜体?

library(ggplot2)
library(gridExtra)
p <- qplot(data = mtcars, wt, mpg)
print(arrangeGrob(p, legend = 
  textGrob("large font size colour blue bold\n small font size colour red italic", 
           rot = -90, vjust = 1)))

感谢您的帮助。

您需要将文本分成两个 textGrob

library(ggplot2)
library(gridExtra)
p <- qplot(data = mtcars, wt, mpg)
t1 <- textGrob("small font size colour red italic",
               gp = gpar(fontsize = 12, col = 'red', fontface = 'italic'), 
               rot = -90, vjust = 1)
t2 <- textGrob("large font size colour blue bold", 
               gp = gpar(fontsize = 20, col = 'blue', fontface = 'bold'), 
               rot = -90, vjust = 1)
print(arrangeGrob(p, t1, t2, widths = c(9/10, 1/20, 1/20), nrow = 1))

使用expressionatop的解决方案:

p <- qplot(data = mtcars, wt, mpg)
print(arrangeGrob(p, legend=
      textGrob(expression(atop("large font size colour blue bold\n", atop(italic("small font size colour red italic")))),
      rot = -90, vjust = 1, gp=gpar(fontsize=16,fontface="bold"))))