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

问题描述 投票:1回答:1

是否可以用不同的字体制作导图,关于一个因子?我试图绘制我的多变量数据,除了颜色代码外,我还想把一些导图用粗体表示。

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

这就产生了

iris boxplot

我试着只突出一个物种

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

但 "矢量化输入到 element_text() 是官方不支持的。"

有没有办法自定义传说中的个人物品?

r ggplot2 customization legend
1个回答
0
投票

我想没有办法单独设置主题组件,但是有一个变通的方法,就是使用标签的表达式。这样你就可以让任何一个标签都变粗。

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

创建于2020-05-31,作者: 重读包 (v0.3.0)

© www.soinside.com 2019 - 2024. All rights reserved.