For each of the plots (scatter plot, histogram, boxplot, area chart, heat map, correlogram) explain what you see (including what is on the x- and y-axis) and try to transform what you see into insight about the data. All except the correlogram use ggplot2
for plotting. If you want to read more about the idea behind ggplot2
(grammar of graphics) Chapter 3 of R for Data Science is a good read.
install.packages("car")
install.packages("faraway")
install.packages("ggplot2")
install.packages("GGally")
install.packages("reshape")
install.packages("corrplot")
install.packages("corrgram")
Three different data sets are used - read descriptions in R:
SLID
: ?car::SLID
mtcars
: ?datasets::mtcars
ozone
: ?faraway::ozone
library(car)
library(ggplot2)
SLID = na.omit(SLID)
ggplot(SLID, aes(education, wages)) + geom_point() + labs(title = "Scatterplot") +
theme_minimal()
Solution: The scatterplot shows that the people with the largest wages often are the people with the longest education. The plot also indicates that the variance increases as a function of education, i.e the expected wage vary less for a random person with 0-5 years of education compared to a person with 20 years of education.
ggplot(SLID, aes(education, wages)) + geom_point(aes(color = language)) + scale_x_continuous("Education") +
scale_y_continuous("Wages") + theme_bw() + labs(title = "Scatterplot") +
facet_wrap(~language) + theme_minimal()
Solution: From this plot we see that there are more english speaking people in the dataset. In general, the english speaking people have large education (relatively few people with education < 8 years). Among the people who speak other langauges than french and english, there is a larger amount of people with low education.
ggplot(SLID, aes(wages)) + geom_histogram(binwidth = 2) + labs(title = "Histogram") +
theme_minimal()
Solution: Shows the distributon of wages in the dataset.
ggplot(SLID, aes(language, wages)) + geom_boxplot(fill = "skyblue") + labs(title = "Box Plot") +
theme_minimal()
Solution: The median wage is similar for people speaking english, french and other languages. The \(25\) and $75 $ percentiles are also similar for the three boxplots. However, there are more outliers among the english speaking people: There are many people with wages that are larger than the upper \(95\) percentile.
library(GGally)
ggpairs(SLID) + theme_minimal()
Solution: This plot gives us an overview of the dataset:
ages = cut(SLID$age, breaks = 3)
SLID2 = cbind(SLID, ages)
ggplot(SLID, aes(x = wages, fill = ages)) + geom_area(stat = "bin") + theme_minimal()
Solution: Compares the distribution of wages for different age groups. Young people (red) tend to have lower wages than older people between 31.7 and 51.3 years (green).
library(reshape)
head(mtcars)
carsdf = data.frame(scale(mtcars))
carsdf$model = rownames(mtcars)
cars_melt = melt(carsdf, id.vars = "model")
ggplot(cars_melt, aes(x = variable, y = model)) + geom_raster(aes(fill = value)) +
labs(title = "Heat Map") + scale_fill_continuous(name = "Value") + theme_minimal()
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Solution: Visualization of the data. Shows the values of the different covariates (-1 to 3) for the different car models.
The ozone
data:
library(faraway)
data(ozone)
library(corrplot)
ozonecorr = cor(ozone)
corrplot(ozonecorr)
library(corrgram)
corrgram(ozone, upper.panel = panel.conf)
Solution: Visualizes the correlation between different variables in the dataset. We can for example observe a large, negative correlation between \(ibh\) and \(ibt\) and a large, positive correlation between \(ibt\) and \(O3\).