Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disproportionate differences in alpha/size in ggplot2 when set from dataframe column?

Tags:

r

ggplot2

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.

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.

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.


1 Answers

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.

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.

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=size))+
  scale_size_identity()
like image 188
Didzis Elferts Avatar answered Aug 16 '26 10:08

Didzis Elferts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!