当从dataframe列设置时,ggplot2中的alpha/size的不均衡差异?

时间:2021-04-21 23:41:15

I have this scatter plot:

我有一个散点图

iris$size <- 2
ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=size))

it works fine. Now I want to make just one of the points slightly bigger than the others, so I do:

它将正常工作。现在我只想做一个比其他点稍微大一点的点,所以我这样做:

iris$size[3] <- 2.5
ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=size))

This creates a disproportionate difference in size! The point iris$size[3] is not 20% bigger than the remaining points, it's way bigger (probably 10x). If I simply set size= to some constant, like 2.5, outside the aes() then the size 2.5 looks bigger in the way expected compared to 2.0, but not when it's set within the aes() as a column in the data frame.

这造成了不成比例的大小差异!点iris$size[3]并不比其他点大20%,而是大得多(可能是10倍)。如果我简单地将size=设置为某个常数,比如2.5,在aes()之外,那么与2.0相比,2.5的大小看起来要大一些,但在aes()中将其设置为数据框架中的一列时就不是这样了。

The same is true for alpha=. If I set alpha= as a constant, not within aes() it works fine, but if I set some points to have an alpha of 0.6 and a few to have an alpha of 0.65, the difference gets amplified to be tremendously big.

对于alpha=也一样。如果我把=设为常数,不在aes()中,它可以工作,但是如果我设一些点的值是0。6,有些点的值是0。65,那么差值就会被放大,变得非常大。

How can I get around this? How can I get alpha/size values to be interpreted from a column within aes() just as they do outside of it when calling geom_point()? thanks.

我怎样才能避开这个问题呢?如何从aes()中的列中获得alpha/size值,就像调用geom_point()时在其之外所做的那样?谢谢。

1 个解决方案

#1


3  

When you set size your values gets distributed between preset range - for the scale_size_continuous() default range is from 1 to 6. So the smallest value in your data gets size=1 and largest values gets size=6. Using scale_size_continuous() and argument range= you can get own range, for example, the same as in your original data.

当您设置大小时,您的值将在预设范围之间分布——对于scale_size_continuous(),默认范围是从1到6。数据中最小的值得到size=1最大的值得到size=6。使用scale_size_continuous()和参数range=可以获得自己的范围,例如,与原始数据相同。

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, 
                color=Species, size=size))+
  scale_size_continuous(range=c(2,2.5),breaks=c(2,2.5))

If the data used for size= are actual size values you want to see on plot you can use use scale_size_identity() which will interpret values specified for size= in aes() directly.

如果size=使用的数据是您希望在plot中看到的实际大小值,那么可以使用scale_size_identity(),它将直接解释aes()中为size=指定的值。

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=size))+
  scale_size_identity()

#1


3  

When you set size your values gets distributed between preset range - for the scale_size_continuous() default range is from 1 to 6. So the smallest value in your data gets size=1 and largest values gets size=6. Using scale_size_continuous() and argument range= you can get own range, for example, the same as in your original data.

当您设置大小时,您的值将在预设范围之间分布——对于scale_size_continuous(),默认范围是从1到6。数据中最小的值得到size=1最大的值得到size=6。使用scale_size_continuous()和参数range=可以获得自己的范围,例如,与原始数据相同。

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, 
                color=Species, size=size))+
  scale_size_continuous(range=c(2,2.5),breaks=c(2,2.5))

If the data used for size= are actual size values you want to see on plot you can use use scale_size_identity() which will interpret values specified for size= in aes() directly.

如果size=使用的数据是您希望在plot中看到的实际大小值,那么可以使用scale_size_identity(),它将直接解释aes()中为size=指定的值。

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=size))+
  scale_size_identity()