![](/img/trans.png)
[英]Ggplot2: Create legend using scale_fill_manual() for geom_rect() and geom_line() in one plot?
[英]How to prevent scale_fill_manual + geom_boxplot + geom_rect to screw the legend?
我正在用geom_rect着色多面箱线图的填充。 当我使用scale_fill_manual手动设置填充颜色时,我无法区分是箱形图填充还是geom_rect填充,这使我的图例更加困惑:
mtcars$type <- "Small"
mtcars$type[mtcars$cyl >= 6] <- "Medium"
mtcars$type[mtcars$cyl >= 8] <- "Big"
mtcars$type <- factor(mtcars$type)
mtcars$typeColor <- "black"
mtcars$typeColor[mtcars$cyl >= 8] <- "white"
mtcars$typeColor <- factor(mtcars$typeColor)
p <- ggplot(mtcars, aes(factor(gear), mpg, fill=factor(gear)))
p <- p + scale_x_discrete()
p <- p + geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill=(typeColor)))
p <- p + geom_boxplot()
p <- p + facet_grid(. ~ type)
p <- p + scale_fill_manual( values = c("black" = "black","white" = "white","3" = "green","4" = "red","5" = "blue"), limits=c("3","4","5"))
show(p)
任何提示如何防止这种情况?
告诉geom_rect
不显示其指南:
ggplot(mtcars, aes(factor(gear), mpg, fill=factor(gear))) +
scale_x_discrete() +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill=(typeColor)),
show.legend = FALSE) +
geom_boxplot() +
facet_grid(. ~ type) +
scale_fill_manual(values = c("black" = "black","white" = "white","3" = "green","4" = "red","5" = "blue"),
limits=c("3","4","5"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.