ggplot2中的点的大小在整个图中是否可比? [英] Size of points in ggplot2 comparable across plots?

查看:296
本文介绍了ggplot2中的点的大小在整个图中是否可比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot2生成各种图,其中点的大小与具有相同x和y值的个案数成正比。有没有办法让点的大小在不同的地块上具有不同的值,这些值有不同的值 size



使用假数据的示例:

  df1 = data.frame(x = seq(1:10),
y = c (4,3.8,3.8,3.2,3.1,2.5,2,1.5,1.2,1.3),
size = c(1,20,1,70,100,70,1,1,110,1))

library(ggplot2)

pdf(plot.1.pdf)
ggplot(df1,aes(x = x,y = y,size = size)) + geom_point()
dev.off()

df2 = data.frame(x = seq(1:10),
y = c(4,3.8,3.8,3.2 ,3.1,2.5,2,1.5,1.2,1.3),
size = rep(1,length(y)))
pdf(plot.2.pdf)
ggplot( df2,aes(x = x,y = y,size = size))+ geom_point()
dev.off()

所有 size 等于1的Plot 1中的点比Plot 2中的点 size 等于1.我需要一个版本的点与th相同的值 size 在不同的图中具有相同的大小。谢谢你,

索非亚

解决方案

code> scale_size_identity() - 它将直接解释 size 作为点的单位,所以在这两个图中,值为1的点将是相同的尺寸。但是,如果 size 值很大(如你的情况),这种方法会造成太大的分数。要处理太大的问题,可以使用scale内的转换,例如平方根,参数 trans =sqrt

  ggplot(df1,aes(x = x,y = y,size = size))+ 
geom_point()+ scale_size_identity(trans = sqrt,guide =legend)

ggplot(df2,aes(x = x,y = y,size = size))+
geom_point()+ scale_size_identity(trans = sqrt,guide =legend)




UPDATE



正如@hadley所指出的那样最简单的方法是将 scale_size_continuous()中的 limits = 设置为相同的值以获得相同的大小。

  ggplot(df1,aes(x = x,y = y,size = size))+ geom_point()+ 
scale_size_continuous(limits = c(1,110))
ggplot(d f2,aes(x = x,y = y,size = size))+ geom_point()+
scale_size_continuous(limits = c(1,110))



I am using ggplot2 to produce various plots in which the size of a point is proportional to the number of cases that have the same values of x and y. Is there a way to make the size of the points comparable across different plots that have different values of size?

Example using fake data:

    df1 = data.frame(x = seq(1:10), 
            y = c(4,3.8,3.8,3.2,3.1,2.5,2,1.5,1.2,1.3),
            size = c(1,20,1,70,100,70,1,1,110,1))

    library(ggplot2)

    pdf("plot.1.pdf")
    ggplot(df1, aes(x = x, y = y, size = size)) + geom_point()
    dev.off()

    df2 = data.frame(x = seq(1:10), 
            y = c(4,3.8,3.8,3.2,3.1,2.5,2,1.5,1.2,1.3),
            size = rep(1,length(y)))
    pdf("plot.2.pdf")
    ggplot(df2, aes(x = x, y = y, size = size)) + geom_point()
    dev.off()

The points in Plot 1, which all have size equal to 1, are much larger than the points in Plot 2 for which size equals 1. I need a version of the plots where points with the same value of size have the same size across different plots. Thank you,

Sofia

解决方案

One possibility is to use scale_size_identity() - that will interpret size directly as units of pointsize, so in both plots points with value 1 will be the same size. But this approach will make too large points if size values are big (as in your case). To deal with problem of too big points, you can use transformation inside scale, for example, square root, with argument trans="sqrt".

ggplot(df1, aes(x = x, y = y, size = size)) + 
  geom_point()+scale_size_identity(trans="sqrt",guide="legend")

ggplot(df2, aes(x = x, y = y, size = size)) + 
  geom_point()+scale_size_identity(trans="sqrt",guide="legend")

UPDATE

As pointed out by @hadley, easiest way to achieve this is to set limits= inside scale_size_continuous() to the same values to get identical sizes.

ggplot(df1, aes(x = x, y = y, size = size)) + geom_point()+
  scale_size_continuous(limits=c(1,110))
ggplot(df2, aes(x = x, y = y, size = size)) + geom_point()+
  scale_size_continuous(limits=c(1,110))

这篇关于ggplot2中的点的大小在整个图中是否可比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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