Kim
Kim

Reputation: 90

Edit point size with geom_count (ggplot2, R)

Trying to find a solution to adjust point size when using geom_count. Geom_count enlarges points when points are overlapping. When creating different plots with geom_count, they all show different point sizes (which can be confusing when comparing the plots). I've seen other options in ggplot2 to change point size, but then geom_count is overruled.

Here's an example version of the script:

x <- c(10,30,60,80,80,100,30,40)
y <- c(30,70,50,80,80,80,40,50)
db <- merge(x,y)

ggplot(db, aes(x, y))+
  geom_count(alpha=0.8, colour="steelblue")+
  scale_size_area(breaks = round)+
  theme_bw() + theme(panel.grid.minor = element_blank(), plot.tag.position = "bottomleft")+
  labs(title= "X vs Y",subtitle="       ", tag = "A")

enter image description here

Upvotes: 2

Views: 2256

Answers (1)

teunbrand
teunbrand

Reputation: 38063

If you want to harmonise the point sizes across different plots, you'd need to give it appropriate limits manually. Consider the following example:

library(ggplot2)
library(patchwork)
#> Warning: package 'patchwork' was built under R version 4.0.3

n <- 10000

df1 <- data.frame(
  x = sample(LETTERS[1:5], n, replace = TRUE),
  y = sample(LETTERS[1:5], n, replace = TRUE)
)

df2 <- data.frame(
  x = sample(LETTERS[1:5], n / 10, replace = TRUE),
  y = sample(LETTERS[1:5], n / 10, replace = TRUE)
)

plot1 <- ggplot(df1, aes(x, y)) +
  geom_count()

plot2 <- ggplot(df2, aes(x, y)) +
  geom_count()

The size scales of the two plots are horribly out of sync:

plot1 + plot2

You can manually calculate what the limits of the scales should be and apply these to the different plots.

range <- range(c(table(df1$x, df1$y), table(df2$x, df2$y)))

# '&' operation is about the same as adding this scale to individual plots
plot1 + plot2 & scale_size_area(limits = range)

Created on 2021-03-31 by the reprex package (v1.0.0)

Upvotes: 2

Related Questions