Same dot sizes

R
dataviz
Author

Aurélien Ginolhac

Published

May 17, 2023

library(ggplot2)
suppressPackageStartupMessages(library(dplyr))
library(ek.plot)

This is how to create dots of the same size and optimized width using Eric Koncina package: ek.plot.

dot_size <- 4
set.seed(1)
p <- tidyr::expand_grid(row = letters[1:5], column = as.character(1:3)) |> 
    mutate(size = sample(0:5, size = 15, replace = TRUE)) |> 
    ggplot(aes(x = column, y = row, size = size)) +
    geom_point() +
    geom_point(shape = 21, size = dot_size, fill = NA, colour = "gray") +
    scale_size(range = c(0, dot_size), limits = c(0, 5), breaks = c(0, 2, 5)) +
    theme_bw() +
    theme(
        legend.key.height =  unit(0.5, "lines")
    ) 
p
set_panel_size(p,
                             width = 3 * unit(dot_size * ggplot2:::.pt, "pt"),
                             height = 5 * unit(dot_size * ggplot2:::.pt, "pt")) |> 
    write_plot("panel-size.png")