使用ggplot2
和scale_size_area()
,如何使area = 0.5
的点大小与默认点(size = 0.5
)的大小相对应?
这是一个简单的repex,表明这不是默认行为。我希望黑色和红色的点在中间点(其中area = 0.5
)具有相同的大小:
ggplot(data.frame(area = seq(from = 0, to = 1, length.out = 17), y = 1), aes(x = area, y = y)) +
geom_point(aes(size = area), color = "red") + # Area point
geom_point() + # Default point
scale_size_area("size_area")
我尝试过使用area = area / 2
和scale_size_area(rescaler = NULL)
,但都失败了。
发布于 2020-07-22 06:57:05
您可以在scale_size
中使用range
和limits
参数,以获得更接近您所需的内容:
ggplot(data.frame(area = seq(from = 0, to = 1, length.out = 17), y = 1), aes(x = area, y = y)) +
geom_point(aes(size = area), color = "red") + # Area point
geom_point() + # Default point
scale_size("size_area", range = c(-20, 10))
编辑:
由于这有点老生常谈,而且不可伸缩,更好的方法是首先找出缺省的点大小:
default_size <- ggplot2:::check_subclass("point", "Geom")$default_aes$size
default_size
[1] 1.5
它应该是1.5,除非您手动更改了默认值。现在我们可以重建绘图,并弄清楚大小美学目前是如何映射到area的:
df <- data.frame(area = seq(from = 0, to = 1, length.out = 17), y = 1)
g <- ggplot(df, aes(x = area, y = y)) +
geom_point(aes(size = area), color = "red") + # Area point
geom_point() +
scale_size_area()
g2 <- ggplot_build(g)
g2$data[[1]] %>%
select(x, size)
x size
1 0.0000 0.000000
2 0.0625 1.500000
3 0.1250 2.121320
4 0.1875 2.598076
5 0.2500 3.000000
6 0.3125 3.354102
7 0.3750 3.674235
8 0.4375 3.968627
9 0.5000 4.242641
10 0.5625 4.500000
11 0.6250 4.743416
12 0.6875 4.974937
13 0.7500 5.196152
14 0.8125 5.408327
15 0.8750 5.612486
16 0.9375 5.809475
17 1.0000 6.000000
关系是size = 6*sqrt(x)。为什么是6?因为scale_size_area的默认大小为6。因此,为了使x值0.5映射到1.5大小,我们必须为新的max_size
求解上面的方程,我们得到1.5/sqrt(0.5)。
要自动执行此操作,我们可以执行以下操作:
default_size_val <- 0.5
max_size <- default_size/(sqrt(default_size_val))
ggplot(df, aes(x = area, y = y)) +
geom_point(aes(size = area), color = "red") + # Area point
geom_point() +
scale_size_area(max_size = max_size)
https://stackoverflow.com/questions/63023877
复制相似问题