Hi there!
I am trying to reproduce the "Vaccination rates by country income level" (Covid World Vaccination Tracker - The New York Times) using ggplot:
I specifically love the "gravitation" effect. How the points/circles with similar values are glued together without overlapping.
I've tried geom_point()
, geom_jitter()
, geom_beeswarm()
with different parameters without luck. With enough patience and jittering, you can get somehow close, but not enough.
Is there a geom_point_attract()
similar to ggrepel::geom_label_repel()
to create glued repelled points to reproduce the NYTimes plot?
See below some experimentation:
library(ggbeeswarm)
#> Loading required package: ggplot2
library(tidyverse)
N = 100
DF = tibble(country = sample(c("Low income", "High income"), N, TRUE),
value = sample(c(1, 1.01,1.54, 1.56, 1.87, 1.85, 2, 2.01, 2.02), N, TRUE), #runif(n = N, min = 1, max = 2)
size_dot = round(runif(n = N, min = 1, max = 30), 0))
ggplot(DF, aes(value, country, fill = country )) +
ggbeeswarm::geom_beeswarm(aes(size = size_dot), shape = 21, alpha = .8, groupOnX = FALSE, priority = "random") +
theme_minimal() +
theme(legend.position = "none")
ggplot(DF, aes(value, country, fill = country)) +
geom_point(aes(size = size_dot), shape = 21, alpha = .8, position = position_dodge2(width = .2)) +
theme_minimal() +
theme(legend.position = "none")
ggplot(DF, aes(value, country, fill = country)) +
geom_point(aes(size = size_dot), shape = 21, alpha = .8, position = position_jitterdodge(jitter.width = .07, jitter.height = .05, seed = 5)) +
theme_minimal() +
theme(legend.position = "none")
ggplot(DF, aes(value, country, fill = country)) +
geom_jitter(aes(size = size_dot), shape = 21, alpha = .8, width = .05, height = .05) +
theme_minimal() +
theme(legend.position = "none")
EDIT: solution
OK, it seems this kind of thing is called circle packing (Circular Packing | the R Graph Gallery) and there is a very nice package that helps with it: {packingcircles} (GitHub - mbedward/packcircles: R package for circle packing).
Getting to the NYTimes-type plot is not trivial, so I ended up creating a repo with a few functions to take care of the complexity: GitHub - gorkang/linearpackcircles.
The end result:
Created on 2021-05-14 by the reprex package (v2.0.0)
Thanks!