我制作了一个ggplot图,其中的图例是由2种不同几何图形的映射组成的,如下所示:
# Create data.frame with shading info
shading <- data.frame(min = seq(from = 0.5, to = max(as.numeric(as.factor(mtcars$carb))), by = 1),
max = seq(from = 1.5, to = max(as.numeric(as.factor(mtcars$carb))) + 0.5, by = 1),
col = c(0,1))
# Plot
ggplot() +
geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl))) +
geom_rect(data = shading,
aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
fill = factor(col)), alpha = 0.3) +
scale_fill_manual(values = c("black", "white", "green", "blue", "red")) +
geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl)))
如何从图例中删除前两个图例项(黑色0和白色1),但将其保留在图形中?
谢谢
您可以在scale_fill_manual()
通话中明确设置休息时间-允许您说出哪些会被标记,哪些不会被标记。(我在这里保留了其余的代码)。如果您正在寻找更多信息和演练,请访问:https : //luisdva.github.io/rstats/set-the-breaks/
library(ggplot2)
shading <- data.frame(min = seq(from = 0.5, to = max(as.numeric(as.factor(mtcars$carb))), by = 1),
max = seq(from = 1.5, to = max(as.numeric(as.factor(mtcars$carb))) + 0.5, by = 1),
col = c(0,1))
# Plot
ggplot() +
geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl))) +
geom_rect(data = shading,
aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
fill = factor(col)), alpha = 0.3) +
scale_fill_manual(values = c("black", "white", "green", "blue", "red"),
# Adding breaks
breaks = c(4, 6, 8)) +
geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl)))
由reprex软件包(v0.3.0)创建于2020-07-28
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句