如何将颜色和大小的比例合并为一个图例? [英] How to combine scales for colour and size into one legend?

查看:136
本文介绍了如何将颜色和大小的比例合并为一个图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在尺寸和颜色来自相同数据的情节中为 scale_size(){ggplot2} 中的size_scale着色。



示例:

  library(ggplot2)
df< ; -as.data.frame(cbind(rep(1:10,10),
rep(1:10,each = 10),
rnorm(100)))

ggplot(df,aes(V1,V2))+
geom_point(aes(color = V3,size = V3))+
scale_colour_gradient(low =gray,high =black)) +
scale_size(range = c(1,10))

正如您所见, V3 对于数据点的颜色和大小是相同的。如何将颜色渐变合并到尺寸比例尺中(除非在诸如Illustrator等程序中手动执行此操作)?谢谢!

解决方案使用 guides()函数 GGPLOT2 。在这种情况下:

  ggplot(df,aes(V1,V2))+ 
geom_point(aes(color = V3,size = V3))+
scale_colour_gradient(low =gray,high =black)+
scale_size(range = c(1,10))+
guides(color = guide_legend(),size = guide_legend())

ggplot2会尝试为您整合比例。它不适用于这种情况,因为色阶的默认指南是一个色条,而大小的默认指南是一个正常的图例。一旦你将它们都设置为传说,ggplot 2将接管并合并它们。




I would like to know how to colorize the size_scale in scale_size() {ggplot2} in a plot where the size and color are from the same data.

Example:

 library(ggplot2)
    df<-as.data.frame(cbind(rep(1:10,10),
                            rep(1:10,each=10),
                            rnorm(100)))

    ggplot(df,aes(V1,V2))+
      geom_point(aes(colour=V3,size=V3))+
      scale_colour_gradient(low="grey", high="black")+
      scale_size(range=c(1,10))

As you can see the V3 is the same for the color and the size of the data points. How can I merge the color gradient to the size scale (except do this manually in program such as Illustrator...)? Thanks!

解决方案

Use the guides() function of ggplot2. In this case:

ggplot(df,aes(V1,V2))+
  geom_point(aes(colour=V3,size=V3))+
  scale_colour_gradient(low="grey", high="black")+
  scale_size(range=c(1,10)) +
  guides(color=guide_legend(), size = guide_legend())

ggplot2 will try to integrate the scales for you. It doesn't in this case because the default guide for a color scale is a colorbar and the default guide for size is a normal legend. Once you set them both to be legends, ggplot 2 takes over and combines them.

这篇关于如何将颜色和大小的比例合并为一个图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆