How can I make ggplot2 give a separate legend for different geoms that both use color to represent 2 different variables. I'd want 3 legends, one for z
, a
, and b
, but a & b seem to be combined into a singe legend even though a & b represent different variables. I'd also like to be able to control the colors in each legend.
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"))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…