########################
# R Course WS 2019-20
########################
# Solutions to Exercise sheet 6 - Data visualization and graphics
# Updated by NoĆ©mie 17/2/2020
# Clean R environment ----
rm(list=ls())
# Download Catfish_2.csv file from the R-course web page and load it into R
# Load data file
Catfish_2 <- read.csv("data/Catfish_2.csv",
header = TRUE,
stringsAsFactors = FALSE)
################################################################################
### Exercise 1: Making simple plots in base R
# Plot a histogram of the variable Weight.
# Use a colour of your choice and set the number of bins to 50.
hist(Catfish_2$Weight,
col = "grey",
breaks = 50)
# Plot a histogram that displays only the values for April. As you wish to add a
# density curve make sure that probability densities, instead of frequencies,
# are displayed. Add a blue density line with a width of 1.5 to the plot.
hist(Catfish_2[Catfish_2$Month == "April",]$Weight,
col = "grey",
breaks = 50,
freq = FALSE)
lines(density(Catfish_2[Catfish_2$Month == "April",]$Weight),
col = "blue",
lwd = 1.5)
# Make a scatterplot and plot the length against the weight of each fish. Change
# the label of the x-axis to "Length (cm)" and the label of the y-axis to
# "Weight (g)".
plot(Catfish_2$Length,
Catfish_2$Weight,
xlab = "Length (cm)",
ylab = "Weight (g)")
### Exercise 2: Multiple plots on one page
# Make a subset (Catfish_April) that includes only the measurements for the
# month April and produce a figure, which resembles the multi-panel figure on
# the exercise sheet.
Catfish_April <- subset(Catfish_2, Month == "April")
# Preparation for saving as pdf
pdf("MultiPanelPlot.pdf", width = 7, height = 7)
par(mfrow=c(1,3)) # divide the plotting area into three panels
# plot 1
boxplot(Weight ~ Temperature, data = Catfish_April,
xlab = "Temperature",
names = c("Low","High"),
ylab = "Weight (g)" ,
col = c("white", "lightgrey"),
ylim = c(50,100),
main = "Overall")
# plot 2
boxplot(Weight ~ Temperature,
data = Catfish_April[Catfish_April$Species == "Silurus.glanis",],
xlab = "Temperature",
names = c("Low","High"),
ylab = "Weight (g)" ,
col = c("white", "lightgrey"),
ylim = c(50,100),
main = "Silurus.glanis")
# plot 3
boxplot(Weight ~ Temperature,
data = Catfish_April[Catfish_April$Species == "Silurus.asotus",],
xlab = "Temperature",
names = c("Low","High"),
ylab = "Weight (g)" ,
col = c("white", "lightgrey"),
ylim = c(50,100),
main = "Silurus.asotus")
# Re-set parameters
par(mfrow = c(1,1))
# You have to close the device in the end
dev.off()
### Exercise 3: Graphics with ggplot2
# Install and load package
# install.packages("ggplot2")
library(ggplot2)
ggplot(Catfish_2, aes(Length,
Weight,
colour = Species,
shape = Species)) +
geom_point(size = 2) +
xlab("Length (mm)") +
ylab("Weight (g)") +
theme_classic(base_size = 17)
# Save plot
ggsave("ggplot.pdf", width = 7, height = 7)
### Exercise 4
# Use plot() to produce the following plot of the sine function on the interval
# [- 2*pi, 2*pi]:
pdf("SineFunctionPlot.pdf", width = 7, height = 7)
plot(sin, from = -2*pi, to = 2*pi,
col = "green",
main = "Plotting a continuous function",
sub = "The sine function",
col.main = "blue",
col.sub = "orange",
cex.main = 2,
lty = "longdash",
las = 1)
dev.off()
### Exercise 5
# Load data
data(cars)
attach(cars)
# Figure
pdf("cars.pdf", width = 7, height = 7)
plot(cars,
axes = "FALSE",
main = "Distance taken to stop a car from a certain speed",
sub = "Distance as a function of speed"
)
# Add x axis
axis(1, at = pretty(speed), line = 1)
# Add y axis
axis(2, at = pretty(dist), line = 1)
# Add line
abline(-17.5,4)
# Position of legend without locator(1)
legend("topleft", legend = expression(4%.%speed-17.5), lwd = 2)
dev.off()
detach(cars)