ggplot size aes vs geom_point size aes different results

Hi, I´m trying to make a ggplot and my data are weighted. Depends of the way I do it I have different results. Here is the code:

my_data <- tibble(Var_1 = c(900, 1500, 350, 1200, 750, 100,125,250,300),
                  Var_2 = c(385, 988, 150, 355,555, 900,20, 25, 500),
                  my_weights = c(2.2, 3.1, 8.2, 4.2, 5.3, 6.8, 12, 25, 1))

If I use size inside the ggplot aes () I obtain this

ggplot(my_data, aes(Var_1, Var_2, size = my_weights))+
  geom_point(alpha=1/3)

But if I use the weights inside the geom_point() aes looks like this:

ggplot(my_data, aes(Var_1, Var_2))+
  geom_point( size = my_data$my_weights, alpha=1/3)

I am a bit confused. Should Not be the same result?

Be careful, when to use geom_point and geom_count.

Thanks. Do you know what way is correct or why the results are different?

This has nothing to do with geom_point and geom_count. Instead, you have misspecified the size value by referencing the vector my_data$my_weights outside of the aesthetic:

To determine the size of the points within geom_point():

ggplot(my_data, aes(Var_1, Var_2))+
  geom_point(aes(size = my_weights), alpha=1/3)

If you reference a vector outside of the data specified in the data argument, it will not be interpreted correctly.

1 Like

Thanks for your help.
So this two ways are correct and I have the same output. Did I get it right??

ggplot(my_data, aes(Var_1, Var_2))+
  geom_point(aes(size = my_weights), alpha=1/3)

ggplot(my_data, aes(Var_1, Var_2, size = my_weights))+
  geom_point(alpha=1/3)

And this way is wrong

ggplot(my_data, aes(Var_1, Var_2))+
  geom_point( size = my_data$my_weights, alpha=1/3)

Did I get it right??

Yes that's right. You only want to specify static values outside of the aesthetic. For instance, to specify the size of all points and the alpha value:

ggplot(my_data, aes(Var_1, Var_2))+
  geom_point(aes(color = my_weights), size = 3, alpha=1/3)

Anytime you're referencing a part of your dataframe, you put it inside of the aesthetic.

1 Like

Note also that you can control the size scaling for the points. For example:

You can set the minimum and maximum point sizes using the range argument of scale_size_continuous and you can set the legend breaks with the breaks argument:

ggplot(my_data, aes(Var_1, Var_2, size = my_weights))+
  geom_point(alpha=1/3) +
  scale_size_continuous(range=c(1,5), breaks=c(1,10,20,25))

If you want the my_weights values to be used as the point sizes, use scale_size_identity:

ggplot(my_data, aes(Var_1, Var_2, size = my_weights))+
   geom_point(alpha=1/3) +
   scale_size_identity()
2 Likes

There isn't actually a need to specify size_scale_identity is there? Isn't that the default if you pass a numeric variable to size within aes()?

Since data values can often be huge or tiny compared to practical marker sizes, ggplot scales the marker size range for the size aesthetic so that by default the minimum data value is mapped to a marker size of 1 and the maximum data value is mapped to a marker size of 6. You do need scale_size_identity if you want to use the actual data values.

For example:

p1 = ggplot(my_data, aes(Var_1, Var_2, size = my_weights))+
  geom_point(alpha=1/3) +
  scale_size_identity()

p2 = ggplot(my_data, aes(Var_1, Var_2, size = my_weights))+
  geom_point(alpha=1/3)

list(identity=layer_data(p1)$size %>% range(),
     default=layer_data(p2)$size %>% range())
$identity
[1]  1 25

$default
[1] 1 6
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.