ggplot2:为2个不同的geom分配颜色并获得2个不同的图例

时间:2015-04-09 02:52:17

标签: r ggplot2

如何让 ggplot2 为不同的geom提供单独的图例,这两个geom都使用颜色来表示2个不同的变量。我想要3个图例,一个用于zab,但是一个& b似乎被组合成一个单一的传奇,即使是& b代表不同的变量。我也希望能够控制每个传奇中的颜色。

dat <- data.frame(
    y = rnorm(200),
    x = sample(c("A", "B"), 200, TRUE),
    z = sample(100:200, 200, TRUE), 
    a = sample(c("male", "female"), 200, TRUE),
    b = factor(sample(1:2, 200, TRUE))
)

ggplot(dat, aes(y = y, x = x)) +
    geom_point(aes(color = a, size = z)) + 
    geom_boxplot(fill = NA, size=.75, aes(color=b)) +
    scale_color_manual(values = c("#F8766D", "#00BFC4", "orange", "purple"))

enter image description here

3 个答案:

答案 0 :(得分:12)

如果使用填充的绘图符号,则可以将一个因子映射到填充,另一个映射到颜色,然后将它们分成两个比例,因此将它们分隔为图例。

ggplot(dat, aes(y = y, x = x)) +
  geom_point(aes(fill = a, size = z), pch = 21) + 
  geom_boxplot(fill = NA, size=.75, aes(color=b)) +
  scale_color_manual(values = c("orange", "purple")) +
  scale_fill_manual(values = c("#F8766D", "#00BFC4"))

enter image description here

答案 1 :(得分:3)

似乎传说捕获方法在类似的情况下是最普遍的,尽管在这个特定的@jennybryan上更简单,可能是大多数人想要的。我也在这里记录了图例捕获方法。我首先从@Sandy Muspratt HERE学到了这种方法。

enter image description here

dat <- data.frame(
    y = rnorm(200),
    x = sample(c("A", "B"), 200, TRUE),
    z = sample(100:200, 200, TRUE), 
    a = sample(c("male", "female"), 200, TRUE),
    b = factor(sample(1:2, 200, TRUE))
)

if (!require("pacman")) install.packages("pacman")
pacman::p_load(ggplot2, grid, gridExtra, gtable)

coldot <- ggplot(dat, aes(y = y, x = x)) +
    geom_point(aes(color = a, size = z)) + 
    #geom_boxplot(fill = NA, size=.75, aes(color=b)) +
    scale_color_manual(values = c("#F8766D", "#00BFC4"))

colbox <- ggplot(dat, aes(y = y, x = x)) +
    #geom_point(aes(color = a, size = z)) + 
    geom_boxplot(fill = NA, size=.75, aes(color=b)) +
    scale_color_manual(values = c("orange", "purple"))



leg1 <- gtable_filter(ggplot_gtable(ggplot_build(coldot)), "guide-box") 
leg1Grob <- grobTree(leg1)

leg2 <- gtable_filter(ggplot_gtable(ggplot_build(colbox)), "guide-box") 
leg2Grob <- grobTree(leg2)


noleg <- ggplot(dat, aes(y = y, x = x)) +
    geom_point(aes(color = a, size = z)) + 
    geom_boxplot(fill = NA, size=.75, aes(color=b), position=position_dodge(1)) +
    scale_color_manual(values = c("orange", "purple", "#F8766D", "#00BFC4")) +
    theme(
        plot.margin = unit(c(5.1, 4.1, 4.1, 2.1), "pt"),
        legend.position=c(1.3, 0.87)
    ) +
    guides(color = FALSE)

legs <- ggplot(data = data.frame(x=1, y=1)) +
    geom_blank(aes(x=x, y=y)) + 
    theme_minimal() + 
    ylab(NULL) + xlab(NULL) +
    theme(
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank() 
    ) +
    annotation_custom(leg1Grob, xmin=1, xmax=1, ymin=.95, ymax=1.3) +
    annotation_custom(leg2Grob, xmin=.6, xmax=.8, ymin=.75, ymax=1) 

out <- arrangeGrob(noleg, legs, ncol=2, widths=c(.85, .15))
print(out)

答案 2 :(得分:1)

ggnewscale非常简单:

library(ggplot2)
library(ggnewscale)

ggplot(dat, aes(y = y, x = x)) +
  geom_point(aes(color = a, size = z)) + 
  scale_color_brewer(palette = 'Dark2') +
  new_scale_color() +
  geom_boxplot(fill = NA, aes(color = b)) +
  scale_color_brewer(palette = 'Paired')

reprex package(v0.3.0)于2020-01-08创建

任何scale_color都可以照常使用。为了方便起见,我选择了scale_color_brewer