繁体   English   中英

在 R ggplot2 对象中更改颜色形状和编辑图例

[英]Change color shape and edit legend in R ggplot2 object

我正在尝试修改一个 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_manualscale_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 年 4 月 13 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM