Last active
August 10, 2019 03:06
-
-
Save steveharoz/1c8761c4b79659edeac6850a2ea44a87 to your computer and use it in GitHub Desktop.
Synthesized Anscombe's quartet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(synthpop) | |
# make the built-in anscombe dataset tidy | |
anscombe_tidy = anscombe %>% | |
rowid_to_column("pointNumber") %>% | |
# pivot long | |
gather(setNumber, value, -pointNumber) %>% | |
# get the dimention and set number | |
mutate(dimention = substring(setNumber, 1, 1), setNumber = substring(setNumber, 2)) %>% | |
# combine each point into a row | |
spread(dimention, value) %>% | |
# cleanup | |
dplyr::select(-pointNumber) %>% | |
mutate(setNumber = paste('Set', setNumber)) | |
# plot the data | |
plot_original = ggplot(anscombe_tidy) + | |
aes(x=x, y=y) + | |
geom_point(size = 2, color = "#377EB8", alpha = 0.5) + | |
stat_smooth(geom='line', method = "lm", color = "#E41A1C", alpha = 0.75, se = FALSE, fullrange = TRUE) + | |
facet_grid(. ~ setNumber) + | |
theme_bw() + | |
labs(title = "Anscombe's Quartet") | |
# synethesize each set in the data | |
synthesized_anscombe = anscombe_tidy %>% | |
group_by(setNumber) %>% | |
do(synthpop::syn(., seed = 12345)$syn) | |
# plot the synthesized data | |
plot_synthesized = ggplot(synthesized_anscombe) + | |
aes(x=x, y=y) + | |
geom_point(size = 2, color = "#377EB8", alpha = 0.5) + | |
stat_smooth(geom='line', method = "lm", color = "#E41A1C", alpha = 0.75, se = FALSE, fullrange = TRUE) + | |
facet_grid(. ~ setNumber) + | |
theme_bw() + | |
labs(title = "Synthesized Anscombe's Quartet") | |
# plot both together | |
cowplot::plot_grid(plot_original, plot_synthesized, ncol = 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment