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

Constant label size with geom_point() while using variable point size

I have generated a scatterplot with variable point size using geom_point(). I also wish to label the points, but doing so also adjusts the size of the label. Is it possible to keep a constant label size while varying the point size?

See example code and output below. I want basically this output except with a single consistent label font size.

library(ggplot2)
library(magrittr)

df = data.frame(score1 = rnorm(10),
               score2 = rnorm(10),
               group_size = exp(rnorm(10)),
               group_name = LETTERS[1:10]
)

df %>% ggplot(aes(x = score1, y = score2, size = group_size, label = group_name)) +
  geom_point() +
  geom_text(vjust = 1.8)

enter image description here

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

>Solution :

Move the size aesthetic to geom_point():

ggplot(df, aes(x = score1, y = score2, label = group_name)) +
  geom_point(aes(size = group_size)) +
  geom_text(vjust = 1.8)
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