Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Last active April 29, 2023 21:31
Show Gist options
  • Save benmarwick/8cf22ecb74ac511f8ac1c70aef6038a7 to your computer and use it in GitHub Desktop.
Save benmarwick/8cf22ecb74ac511f8ac1c70aef6038a7 to your computer and use it in GitHub Desktop.
How to make diamond plots after Bergstrom and West (2018) "Why scatter plots suggest causality, and what we can do about it"
# https://arxiv.org/pdf/1809.09328.pdf & https://twitter.com/CT_Bergstrom/status/1035327464644333568
# they use Mathematica, boo! So let's make them with R
# starting with https://stackoverflow.com/q/33396168/1036500
library(ggplot2)
p <- ggplot() +
geom_point(data = anscombe,
aes(x = x1,
y = y1),
colour = "red",
alpha = 0.5,
size = 3) +
theme_minimal() +
theme(axis.text.x= element_text(size = 10),
axis.text.y= element_text(size = 10)) +
theme(axis.text.x = element_text(angle=(-1 * rotation)),
axis.text.y = element_text(angle=(-1 * rotation)),
axis.title.x = element_text(size = 12,
angle=(-1 * rotation),
vjust = 0.5,
margin = margin(t = 10),
colour = "red"),
axis.title.y = element_text(size = 12,
angle=(-1 * rotation),
hjust = 0.5,
margin = margin(r = 20),
colour = "red")) +
coord_equal() +
scale_x_continuous(limits = c(0, 15),
name = "x") +
scale_y_continuous(limits = c(0, 15),
name = "y")
library(grid)
print(p, vp=viewport(angle=rotation,
width = unit(.75, "npc"),
height = unit(.75, "npc")))
@benmarwick
Copy link
Author

How to draw a diamond plot visualization with R and ggplot2

Bergstrom and West 2018 gist of ggplot2 code
image image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment