library(ggplot2)
library(patchwork)
library(tidyr)
library(magrittr)
library(dplyr)
datasets::anscombe
#> x1 x2 x3 x4 y1 y2 y3 y4
#> 1 10 10 10 8 8.04 9.14 7.46 6.58
#> 2 8 8 8 8 6.95 8.14 6.77 5.76
#> 3 13 13 13 8 7.58 8.74 12.74 7.71
#> 4 9 9 9 8 8.81 8.77 7.11 8.84
#> 5 11 11 11 8 8.33 9.26 7.81 8.47
#> 6 14 14 14 8 9.96 8.10 8.84 7.04
#> 7 6 6 6 8 7.24 6.13 6.08 5.25
#> 8 4 4 4 19 4.26 3.10 5.39 12.50
#> 9 12 12 12 8 10.84 9.13 8.15 5.56
#> 10 7 7 7 8 4.82 7.26 6.42 7.91
#> 11 5 5 5 8 5.68 4.74 5.73 6.89
plot_fn = function(x,y){
maxx=max(anscombe[[x]])
maxy=max(anscombe[[y]])
ggplot(anscombe, aes(eval(get(x)), eval(get(y)))) +
geom_point(color='red', size=2.3) +
geom_smooth(method='lm', se=FALSE) +
labs(x=x,y=y) +
scale_x_continuous(limits = c(0,maxx + 2), expand=c(0,0)) +
scale_y_continuous(limits = c(0, maxy + 2), expand=c(0,0)) +
theme_classic()
}
p1 = plot_fn('x1', 'y1')
p2 = plot_fn('x2', 'y2')
p3 = plot_fn('x3', 'y3')
p4 = plot_fn('x4', 'y4')
(p1 + p2)/(p3 + p4) + plot_annotation(title = 'Anscombe Quartet',
theme = theme(plot.title = element_text(hjust=0.5)))
anscombe %>%
pivot_longer(cols=everything(),
names_to='variables',
values_to='values') %>%
group_by(variables) %>%
summarise('mean'=mean(values), 'variance'=var(values))
#> # A tibble: 8 x 3
#> variables mean variance
#> <chr> <dbl> <dbl>
#> 1 x1 9 11
#> 2 x2 9 11
#> 3 x3 9 11
#> 4 x4 9 11
#> 5 y1 7.50 4.13
#> 6 y2 7.50 4.13
#> 7 y3 7.5 4.12
#> 8 y4 7.50 4.12
Last active
September 6, 2020 11:40
-
-
Save mohit2152sharma/78ae60fea62c48a6efa5ab0319b1d69e to your computer and use it in GitHub Desktop.
anscombe quartet plotting and summarizing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment