如何使用ggplot将额外的形状项添加到填充图例中?

时间:2017-12-02 21:07:57

标签: r ggplot2 legend boxplot legend-properties

我制作了一个填充的箱形图,然后我放了一个红十字来突出显示组的意思。如果图例是使用scale_fill_manual构建的,如何将红十字添加到图例中。我尝试使用scale_shape_manual,但它没有用。

library(ggplot2)

Chemical <- rep(c("NaCl", "Al2"), times = 3, each = 4)
Quality <- rep(c("Taste", "Color\nof package", "Capacity"), times = 1, each = 8)
Accepted <- seq(0, 100, by = 100/23)

DF <- data.frame(Chemical, Quality, Accepted)
means <- aggregate(Accepted ~ Chemical + Quality, DF, mean)

ggplot(DF, aes(x = Quality, y = Accepted, fill = Chemical)) +
  geom_boxplot() +
  scale_fill_manual(values = c("orange", "green", "red"),
                     labels = expression("Al"[2], "NaCl", "Means")) +
  geom_point(data = means, aes(x = Quality, y = Accepted),
             position = position_dodge(0.75), shape = 3, color = 2) +
  xlab("") +
  theme(legend.position = "top", legend.title = element_blank())

enter image description here

0 个答案:

没有答案