Creating plots creates great visuals for data to be more digestible. It helps data become readable and the information to be obtain quicker through a visual.In some situations when there is a need for comparison it is beneficial to create plots and compare them side to side. In this explanation, we will create plots and use the R package gridExtra’s function grid.arrange to combine two plots side by side.
The data we will be using in this tutorial will be simulated data with variables in class size, test scores, socioeconomic status, and teacher quality. We would like to understand how the relationship between class size, teacher quality, and socioeconomic effects test scores.
knitr::opts_chunk$set(echo = TRUE)
URL <- "https://raw.githubusercontent.com/DS4PS/cpp-523-fall-2019/master/labs/class-size-seed-1234.csv"
dat <- read.csv( URL )
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.1
## Warning: package 'ggplot2' was built under R version 4.3.1
## Warning: package 'tidyr' was built under R version 4.3.1
## Warning: package 'purrr' was built under R version 4.3.1
## Warning: package 'dplyr' was built under R version 4.3.1
## Warning: package 'forcats' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” dplyr 1.1.2 âś” readr 2.1.4
## âś” forcats 1.0.0 âś” stringr 1.5.0
## âś” ggplot2 3.4.3 âś” tibble 3.2.1
## âś” lubridate 1.9.2 âś” tidyr 1.3.0
## âś” purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rsconnect)
## Warning: package 'rsconnect' was built under R version 4.3.2
Let’s say we want to create plots to see if class size or teacher quality has an effect on test score. We will first create a scatter plot that has class size and test scores.
p1 <- dat %>%
ggplot(aes(x=csize, test))+
geom_point()+
theme(legend.position="top")+
guides(color=guide_legend(ncol=1))
p1
Looking at the scatter plot, we can see that increasing class size has a direct effect on decreasing test scores.
Now we will create our second scatter plot which will be teacher quality and test scores.
p2 <- dat %>%
ggplot(aes(x=tqual, test))+
geom_point()+
theme(legend.position="top")+
guides(color=guide_legend(ncol=1))
p2
In this scatter plot it is clear increasing teacher quality will incease test scores.
Let’s say for example we would like to create a visual to create a comparison with the the two variables for test scores. We want to see if a certain variable is a better indicator on test scores than the other. Finding this out, would help deciding what steps to take next.
gridExtra::grid.arrange(p1,p2, ncol=2)
Once we use the grid.arrange function, we have both scatter plots side by side. It makes it easier to see that teacher quality has more of an effect on test score than class size. From there, the researcher or who ever is viewing the data would know. In order to increase test scores, they would need to increase teacher quality.
We can add the last variable which is socioeconomic factor to see if that has an effect on test scores.
p3 <- dat %>%
ggplot(aes(x=ses, test))+
geom_point()+
theme(legend.position="top")+
guides(color=guide_legend(ncol=1))
p3
Increasing socioeconomic factors will increase test scores.
Adding all three plots will help anyone that wants to view all the variables effects on test scores easily. It is clear that teacher quality has the most effect on test scores.
gridExtra::grid.arrange(p1,p2,p3, ncol=2)
A simple function can help create better visuals and create a more appealing report.