【问题标题】:Size of points in ggplot2 comparable across plots?ggplot2 中点的大小可在不同地块之间进行比较?
【发布时间】:2013-04-15 03:50:44
【问题描述】:

我正在使用 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()

地块 1 中的所有点都具有 size 等于 1,远大于地块 2 中 size 等于 1 的点。我需要一个版本的地块,其中点具有相同的值size 在不同的地块上具有相同的大小。谢谢,

索菲亚

【问题讨论】:

  • 在 ggplot2 中,您处理此问题的方式与确保各图的轴相同:设置大小比例的限制。

标签: r ggplot2


【解决方案1】:

一种可能性是使用scale_size_identity() - 这会将size 直接解释为点大小的单位,因此在两个图中,值为1 的点将具有相同的大小。但是,如果 size 值很大(如您的情况),这种方法会产生太大的影响。对于点太大的问题,可以使用尺度内变换,例如平方根,参数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")

更新

正如@hadley 所指出的,实现此目的的最简单方法是将limits= 内的scale_size_continuous() 设置为相同的值以获得相同的大小。

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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2011-03-05
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多