我可以自定义 ggplot 图例中的个人项目吗?

Can I customize individuals items in a ggplot legend?

是否可以制作不同字体的指南,考虑因素?我正在尝试绘制我的多变量数据,除了颜色代码之外,我还想将一些指南设为粗体。 这是一个可重现的例子

  library("ggpubr")
  library("reshape2")

iris.melt <- melt(iris)

ggboxplot(data = iris.melt, x = "variable", y= "value", add = "jitter",
          add.params = list(color = "Species"), legend = "bottom") +
theme(legend.text = element_text(face = "italic")) +
guides(col = guide_legend(override.aes = list(size=2), label.position = "bottom")) 

产生

我试着只突出显示一个物种

ggboxplot(data = iris.melt, x = "variable", y= "value", add = "jitter",
          add.params = list(color = "Species"), legend = "bottom") +
theme(legend.text = element_text(face = c("plain","italic","plain")) +
guides(col = guide_legend(override.aes = list(size=2), label.position = "bottom"))

但是"Vectorized input to element_text() is not officially supported."

有没有办法自定义图例中的个别项目?

我认为没有单独设置主题组件的方法,但有一个解决方法,即对标签使用表达式。这样您就可以将任何单个标签设为粗体:

library("ggpubr")
library("reshape2")

iris.melt <- melt(iris)

one_bold_label <- expression("Setosa", bold(paste("Versicolor")), "Virginica")

ggboxplot(data = iris.melt, x = "variable", y= "value", add = "jitter",
          add.params = list(color = "Species"), legend = "bottom") +
  theme(legend.text = element_text(face = "italic")) +
  scale_colour_manual(labels = one_bold_label, values = c("#F8766D", "#00BA38", "#619CFF")) +
  scale_fill_manual(labels = one_bold_label, values = c("white", "white", "white"))

reprex package (v0.3.0)

于 2020-05-31 创建