geom_scatterpie带有不缩放的图例

huangapple go评论71阅读模式
英文:

geom_scatterpie with a legend that is unscaled

问题

我想要在地图上按样本大小(n)来缩放我的饼图的半径。然而,n 的方差很大,所以我想要在地图上重新缩放它,比如从 0 到 1(这可以是对数,或者任何其他方式)。我想要图例显示缩放后的大小,但给出实际的 n 值,而不是比例尺。

所以,在下面的示例中,图例会显示宽度从 0 到 1 的圆圈,但每个圆圈上的刻度标记都标有从 0 到 500。因此,观看者可以理解圆圈是按比例缩放的,但可以感知到实际的样本大小,而不仅仅是相对的、缩放后的样本大小。

例如:

library(ggplot)
library(scatterpie)

df <- data.frame(id = c("a", "b", "c"), 
                 lat = c(30, 31, 32), 
                 long = c(-85, -86, -87),
                 a = c(1, 3, 5),
                 b = c(8, 7, 4),
                 c = c(1, 0, 1),
                 n = c(1, 200, 500))

# 绘制不进行缩放的情况下,饼图的大小将无法适应地图(坐标轴太大)
ggplot() +
  geom_scatterpie(data = df,
                  aes(y = lat, x = long,
                      group = id,
                      r = n),
                  cols = c("a", "b", "c")) +
  geom_scatterpie_legend(df$n, x = -85.5, y = 32.5)

# 绘制重新缩放大小的情况也有点大,但重要的是图例显示了缩放后的值

# 将 n 重新缩放到 0 到 1
df$r <- scales::rescale(df$n, to = c(0, 1))

ggplot() +
  geom_scatterpie(data = df,
                  aes(y = lat, x = long,
                      group = id,
                      r = r),
                  cols = c("a", "b", "c")) +
  geom_scatterpie_legend(df$r, x = -85.5, y = 32.5)

geom_scatterpie 中是否有更好的重新缩放的方法,以便 1)适应大方差的比例,使所有饼图可见,2)图例显示实际值而不是缩放后的值?

英文:

I would like to have the radius of my piecharts scaled by sample size (n) on a map. However, the variance in n is large, so I would like rescale it on the map, say from 0 to 1 (this could be a log, or anything really). I would like the legend to show the scaled sizes, but give the actual values of n, not the scales.

So, in the example below, the legend would show circles of widths 0 to 1, but with each tick mark on the circles labelled 0 through 500. Thus the viewer would understand that the circles are scaled, but could get a sense of the actual sample size, not just the relative, scaled sample size.

For example:

library(ggplot)
library(scatterpie)

df &lt;- data.frame(id = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;), 
                 lat = c(30, 31, 32), 
                 long = c(-85, -86, -87),
                 a = c(1, 3, 5),
                 b = c(8, 7, 4),
                 c = c(1, 0, 1),
                 n = c(1, 200, 500))

# Plotting without scaling yields pies that will not fit on a map (the axes are too large)
ggplot()+
  geom_scatterpie(data = df,
                  aes(y = lat, x = long,
                      group = id,
                      r = n),
                      cols = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;))+
  geom_scatterpie_legend(df$n, x = -85.5, y = 32.5)

# Plotting the rescaled size is also a bit big, but importantly shows the scaled values in the legend

# Rescale n to 0 to 1
df$r &lt;- scales::rescale(df$n, to = c(0, 1))

ggplot()+
  geom_scatterpie(data = df,
                  aes(y = lat, x = long,
                      group = id,
                      r = r),
                  cols = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;))+
  geom_scatterpie_legend(df$r, x = -85.5, y = 32.5)

Is there a better way to rescale in geom_scatterpie so 1) scales with large variance allow all pies to be visible, and 2) the legend shows the actual values, not the scaled values?

答案1

得分: 1

你可以通过简单地将计数除以aes中的最大计数来手动重新缩放。在geom_scatterpie_legend中,使用labeller参数进行反转:

ggplot()+
  geom_scatterpie(data = df,
                  aes(y = lat, x = long,
                      group = id,
                      r = n / max(n)),
                  cols = c("a", "b", "c")) +
  geom_scatterpie_legend(df$n/max(df$n), labeller = function(x) x * max(df$n),
                         x = -84.5, y = 31) +
  coord_equal()

geom_scatterpie带有不缩放的图例

英文:

You can manually rescale by simply dividing the count by the maximum count inside aes. This is reversed inside geom_scatterpie_legend using the labeller argument:

ggplot()+
  geom_scatterpie(data = df,
                  aes(y = lat, x = long,
                      group = id,
                      r = n / max(n)),
                  cols = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)) +
  geom_scatterpie_legend(df$n/max(df$n), labeller = function(x) x * max(df$n),
                         x = -84.5, y = 31) +
  coord_equal()

geom_scatterpie带有不缩放的图例

huangapple
  • 本文由 发表于 2023年6月15日 21:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定