热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用大小映射和density2d的不寻常的图例-Unusuallegendusingsizemappinganddensity2d

Iamtryingtomakeascatterplotinggplot2withasizemappingtoathirdvariableanddensity2dc

I am trying to make a scatterplot in ggplot2 with a size mapping to a third variable and density2d contours. It seems as if the legend is being confused by the inclusion of density2d contours.

我试图在ggplot2中制作一个散点图,其大小映射到第三个变量和density2d轮廓。似乎传说因包含density2d轮廓而感到困惑。

For example, the following code works:

例如,以下代码有效:

library('ggplot2')
set.seed(1)
x=rnorm(100); y=rnorm(100,sd=10); z=seq(1,10,length.out=100)
dd=data.frame(x=x,y=y,z=z)
ggplot(dd,aes(x,y,size=z))+geom_point()

plot looks normal

But now, note the legend behaves unusually when I add in a call to stat_density2d(). In particular, the plot legend shows blue blocks instead of black circles:

但现在,请注意当我添加对stat_density2d()的调用时,图例的行为异常。特别是,图例显示蓝色块而不是黑色圆圈:

ggplot(dd,aes(x,y,size=z))+geom_point()+stat_density2d()

plot legend shows blue blocks instead of black circles

1 个解决方案

#1


6  

As size= is one of the aesthetics you can set for the stat_density2d() and in this case it is set in ggplot() call, legend is made for both - lines and points (points are hided under lines in legend as geom_point() is called before stat_density2d()). To remove blue lines from legend, you can set manually size=0.5 (or some other value) inside the stat_density2d() and then legend will be correct.

因为size =是你可以为stat_density2d()设置的美学之一,在这种情况下,它是在ggplot()调用中设置的,传说是针对两个行 - 点和点(点在图例中的行下隐藏为geom_point()在stat_density2d()之前调用。要从图例中删除蓝线,您可以在stat_density2d()内手动设置size = 0.5(或其他一些值),然后图例将是正确的。

ggplot(dd,aes(x,y,size=z))+geom_point()+stat_density2d(size=0.5)

enter image description here


推荐阅读
author-avatar
咔西咔嘻
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有