我正在尝试修改ggplot2对象,在其中我拿到一个对象并使用scale_colour_manual和scale_shape_manual更改了颜色和形状。但是,现在我只想显示图例中的前两项。
正如您在代码中看到的那样,我更改了第一个和第三个形状以使其相互匹配,现在第三个形状是多余的,但我仍然希望绘图中包含基础数据。期望的结果将是plot2,但图例中没有第三个值,但数据仍留在图中。
ggplot(mtcars, aes(x=wt, y=mpg, group=as.factor(cyl))) +
geom_point(aes(shape=as.factor(cyl), color=as.factor(cyl)))
plot <- ggplot(mtcars, aes(x=wt, y=mpg, group=as.factor(cyl))) +
geom_point(aes(shape=as.factor(cyl), color=as.factor(cyl)))
plot2 <- plot +
scale_colour_manual(values = c('#999999', '#999999','#999999')) +
scale_shape_manual(values = c(0, 1, 0))
plot2
添加breaks = ...
到scale_colour_manual
和scale_shape_manual
应该做的
plot3 <- plot +
scale_colour_manual(values = c('#999999', '#999999','#999999'),
breaks = c('4', '6')) +
scale_shape_manual(values = c(0, 1, 0),
breaks = c('4', '6'))
# https://github.com/thomasp85/patchwork
# install.packages("devtools", dependencies = TRUE)
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
plot2 + plot3 +
plot_layout(ncol = 2)
由reprex软件包(v0.2.1.9000)创建于2019-04-13
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句