本文介绍了删除ggplot2中的额外图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个简单的数据框,我试图用ggplot2做一个联合的线和点图。假设我的数据如下所示: df group = c(rep(a,10),rep(b,10))) 我试图做一个情节: g g< ggplot(df,aes(x = x,y = y,group = group) - g + geom_point(aes(color = group,alpha = .8))g 除了一个例外,结果看起来很好。它有一个额外的图例,显示了我的 geom_point 图层的alpha。 如何让图例显示组颜色,但不是一个显示我的alpha设置?解决方案美学可以 set 或 em>在 ggplot 调用中。 美元也可以设置为单个值,方法是在 aes()。 在这种情况下,您希望 / em> alpha = 0.8 和 map color = group 。 要做到这一点, $ b 将 alpha = 0.8 放在 aes()定义。 g g g g 对于任何映射变量,您可以通过使用 guide ='none'来禁用图例的外观c $ c>在适当的比例_... 调用中。例如, g2 geom_line(aes(color = group))+ geom_point(aes(color = group,alpha = 0.8)) g2 + scale_alpha(guide ='none') 哪一个会返回一个相同的图 编辑 @ Joran的评论很有用,我的回答更加全面。 I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this:df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20), group=c(rep("a",10),rep("b",10)))And I'm trying to make a plot:g <- ggplot(df, aes(x=x, y=y, group=group))g <- g + geom_line(aes(colour=group))g <- g + geom_point(aes(colour=group, alpha = .8))gThe result looks fine with one exception. It has an extra legend showing the alpha for my geom_point layer.How can I keep the legend showing group colors, but not the one that shows my alpha settings? 解决方案 Aesthetics can be set or mapped within a ggplot call.An aesthetic defined within aes(...) is mapped from the data, and a legend created.An aesthetic may also be set to a single value, by defining it outside aes().In this case, it appears you wish to set alpha = 0.8 and map colour = group.To do this,Place the alpha = 0.8 outside the aes() definition. g <- ggplot(df, aes(x = x, y = y, group = group))g <- g + geom_line(aes(colour = group))g <- g + geom_point(aes(colour = group), alpha = 0.8)gFor any mapped variable you can supress the appearance of a legend by using guide = 'none' in the appropriate scale_... call. eg.g2 <- ggplot(df, aes(x = x, y = y, group = group)) + geom_line(aes(colour = group)) + geom_point(aes(colour = group, alpha = 0.8))g2 + scale_alpha(guide = 'none')Which will return an identical plotEDIT@Joran's comment is spot-on, I've made my answer more comprehensive 这篇关于删除ggplot2中的额外图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 03:53