有没有办法在ggplot2中保持对尺寸范围的绝对控制 [英] Is there a way to maintain absolute control over the size scale in ggplot2
问题描述
我想根据特定字段的值来指定点的大小,但是我希望能够具有跨越一系列独立生成的图表的大小比例。
b$ b
基本上我希望能够说出X单位的值应该以Y值表示。
下面是一个例子:
library(ggplot2)
df_1< - data.frame(x = c(1:3),y = rep(1,3),size = 10 * c(1:3))
df_2 < - data.frame(x = rep(1,3),size = 100 * c(1:3))
df_1_plot <-ggplot(df_1,aes(x = x,y = y,size = size))+
geom_point()
df_2_plot< - ggplot(df_2,aes(x = x,y = y,size = size))+
geom_point()
df_1_plot会生成与df_2_plot不同比例的图表,但df_2大小字段为df_1大小的10倍:
我在df_2_plot中查找的点大小比df_1_plot中的点大10倍。
解决方案 实现这一目标的一种方法是手动调整比例以使用 scale_size_continuous()
以及指定的比例常量将它们修复为原始数据帧比例 - 任何常量都可以工作,尽管使用 min(df_1 $ size)
可以使尺寸更易于管理。这样每个点的大小都与相同的任意常量有关(在这种情况下 min(df_1 $ size)
)
<代码将如下所示:
ggplot(df_1,aes(x = x,y = y,size =大小))+
geom_point()+
scale_size_continuous(范围= c(min(df_1 $ size)/ min(df_1 $ size),max(df_1 $ size)/ min(df_1 $ size))) )
ggplot(df_2,aes(x = x,y = y,size = size))+
geom_point()+
scale_size_continuous(range = c(min(df_2
$ $ b 然而,正如lukeA所提到的,使用 scale_size_identity()
而不是 scale_size_continuous()
更优雅的方式。
I would like to specify the size of a point based on the value of a particular field, however I'd like to be able to have a size scale that spans across a series of independently generated charts.
Essentially I would like to be able to say that a value of X units should be displayed with a point of size Y.
An example is below
library(ggplot2)
df_1 <- data.frame(x=c(1:3),y=rep(1,3),size=10*c(1:3))
df_2 <- data.frame(x=c(1:3),y=rep(1,3),size=100*c(1:3))
df_1_plot <- ggplot(df_1,aes(x=x,y=y,size=size)) +
geom_point()
df_2_plot <- ggplot(df_2,aes(x=x,y=y,size=size)) +
geom_point()
df_1_plot will produce a chart on a different scale to df_2_plot, although df_2 size field is 10 times the size of df_1's:
I'm looking for the point size in the df_2_plot to be 10 times larger than the points in the df_1_plot.
解决方案 One way to achieve this is to adjust the scales manually to fix them to the original data frames scale using scale_size_continuous()
along with a specified scaling constant - any constant should work, although using min(df_1$size)
keeps the sizes more manageable. That way every point is sized in relation to the same arbitrary constant (in this case min(df_1$size)
)
The code would then look something like:
ggplot(df_1,aes(x=x,y=y,size=size)) +
geom_point() +
scale_size_continuous(range = c(min(df_1$size)/min(df_1$size), max(df_1$size)/min(df_1$size)))
ggplot(df_2,aes(x=x,y=y,size=size)) +
geom_point() +
scale_size_continuous(range = c(min(df_2$size)/min(df_1$size), max(df_2$size)/min(df_1$size)))
However as lukeA mentioned using scale_size_identity()
instead of scale_size_continuous()
achieves the equivalent result in a much more elegant way.
这篇关于有没有办法在ggplot2中保持对尺寸范围的绝对控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!