【问题标题】:create a gradient of size and color of the points in ggplot2 extending beyond the values present在 ggplot2 中创建超出现有值的点的大小和颜色渐变
【发布时间】:2017-10-16 23:02:04
【问题描述】:

我正在构建一个图,其中每个点都有一个基于两个单独列中的线性渐变的大小和颜色

df1 <- data.frame (x = c(1:10), y = c(1:10), pointSize = 1:10, pointCol = 1:10)

ggplot(df1, aes(x = x, y = y, colour = pointCol, size = pointSize)) + geom_point() +
   scale_colour_gradient(low = "steelblue", high = "yellow")

表中编码颜色和点大小的列的最大值为 10。我可以更改渐变大小,使其从 say = 10 变为 20?

【问题讨论】:

  • 不确定您在问什么,颜色将基于数据和低梯度和高梯度。您是否尝试添加不同的颜色? scale_color_gradientn
  • scale_size_continuous(range = c(10,20))?也许尝试描述您的理想输出,与当前输出相关,以便澄清......
  • 抱歉没有更清楚。我有两个散点图,我希望它们具有相同的点大小比例,但我不能将它们绘制在一起(我已经有太多的刻面了)。所以我有一个问题,因为在情节 1 中最大的尺寸值为 10,而在情节 2 中它是 20。但我希望与尺寸值 10 对应的尺寸在两个情节上都相同

标签: r ggplot2


【解决方案1】:

您可以为两个图定义相同的大小比例限制。这是一个例子:

样本数据集:

df1 <- data.frame (x = 1:10, y = 1:10, pointSize = 1:10, pointCol = 1:10)
df2 <- data.frame (x = 1:10, y = 10:1, pointSize = 11:20, pointCol = 1:10)

没有标准化尺寸比例的绘图(我们可以看到绘图 1 中的尺寸 10 与绘图 2 中的尺寸 20 匹配):

p1 <- ggplot(df1, aes(x = x, y = y, colour = pointCol, size = pointSize)) + 
  geom_point() +
  scale_colour_gradient(low = "steelblue", high = "yellow") +
  theme_bw()

p2 <- ggplot(df2, aes(x = x, y = y, colour = pointCol, size = pointSize)) + 
  geom_point() +
  scale_colour_gradient(low = "steelblue", high = "yellow") +
  theme_bw()

gridExtra::grid.arrange(p1, p2, nrow = 1)

现在让我们为两者定义相同的比例限制:

size.limits <- range(df1$pointSize, df2$pointSize)

> size.limits
[1]  1 20

为每个图添加大小比例(我们现在可以看到两个图共享相同的大小图例,图 1 中的最大点与图 2 中的最小点大小相同):

gridExtra::grid.arrange(p1 + scale_size(limits = size.limits), 
                        p2 + scale_size(limits = size.limits), 
                        nrow = 1)

【讨论】:

    猜你喜欢
    • 2021-02-12
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多