删除ggplot中图例键周围的填充

 爱-雨轩_627 发布于 2023-02-04 13:58

我想删除图例周围的灰色矩形.我尝试了各种方法,但都没有.

ggtheme <- 
theme(
axis.text.x = element_text(colour='black'),
axis.text.y = element_text(colour='black'),
panel.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_rect(colour='black', fill=NA),
strip.background = element_blank(),
legend.justification = c(0, 1),
legend.position = c(0, 1),
legend.background = element_rect(colour = NA),
legend.key = element_rect(colour = "white", fill = NA),
legend.title = element_blank()
)

colors <- c("red", "blue")
df <- data.frame(year = c(1:10), value = c(10:19), gender = rep(c("male","female"),each=5))
ggplot(df, aes(x = year, y = value)) + geom_point(aes(colour=gender))  +
stat_smooth(method = "loess", formula = y ~ x, level=0, size = 1, 
    aes(group = gender, colour=gender)) +
ggtheme + scale_color_manual(values = colors) 

在此输入图像描述

3 个回答
  • theme_set(theme_gray() + theme(legend.key=element_blank())) 
    

    如果你还想删除灰色背景:

    theme_set(theme_bw() + theme(legend.key=element_blank())) 
    

    2023-02-04 14:01 回答
  • + theme(legend.background=element_blank())
    

    2023-02-04 14:01 回答
  • 你在图例键中得到这种灰色的颜色,因为你使用stat_smooth()它作为默认值也会在线条周围产生一些填充的置信区间(如果fill=在里面没有使用,则为灰色aes()).

    一个解决方案是设置se=FALSEstat_smooth(),如果你不需要的置信区间.

      +stat_smooth(method = "loess", formula = y ~ x, level=0, size = 1, 
                  aes(group = gender, colour=gender),se=FALSE) 
    

    另一个解决方案是使用该功能guides()override.aes=从图例中删除填充,但保持线周围的置信区间.

      + guides(color=guide_legend(override.aes=list(fill=NA)))
    

    2023-02-04 14:02 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有