Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot2::scale_size_area() not mapping zero to zero area?

From the documentation of ggplot2::scale_size_area(), I read that

scale_size_area() ensures that a value of 0 is mapped to a size of 0.

However, in this simple example, I still see points where there zeros are. Is this a bug or am I interpreting this wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(ggplot2)

table(1:3, 1:3) |>
  as.data.frame() |>
  ggplot() +
  aes(x=Var1, y=Var2, size=Freq) +
  geom_point() +
  scale_size_area()

Data

table(1:3, 1:3) |>
  as.data.frame() |>
print()
  Var1 Var2 Freq
1    1    1    1
2    2    1    0
3    3    1    0
4    1    2    0
5    2    2    1
6    3    2    0
7    1    3    0
8    2    3    0
9    3    3    1

Result

enter image description here

>Solution :

I think the issue here is that ggplot2 is drawing a point with zero area but non-zero stroke, so it is effectively drawing a tiny circle around nothing.

The shapes in geom_point() typically have both an outline and a fill; shapes 19-25 give you separate control of these, but they all have a stroke parameter to control how heavy the outline appears (if at all).

Compare

ggplot(data.frame(a = 0:5), aes(a, a, size = a)) +
  geom_point() +
  scale_size_area()

enter image description here

ggplot(data.frame(a = 0:5), aes(a, a, size = a)) +
  geom_point(stroke = 0) +
  scale_size_area() 

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading