library(ggplot2)
temp = read.csv("Temperature.csv")GEOG 5680 Module 7: Better Plotting with ggplot2
Introduction
Use ggplot2 to explore temperature and salinity data.
Histograms of Salinity
Overall Salinity
myplot = ggplot(temp, aes(x = Salinity))
myplot +
geom_histogram()`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Warning: Removed 798 rows containing non-finite outside the scale range
(`stat_bin()`).
Salinity by Year
myplot = ggplot(temp, aes(x = Salinity))
myplot + geom_histogram(binwidth = 0.5,
color = "lightblue",
fill = "white") +
facet_wrap(~ Year)Warning: Removed 798 rows containing non-finite outside the scale range
(`stat_bin()`).
Salinity by Month
temp$Month = factor(temp$Month,
levels = 1:12,
labels = c("Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"))
myplot = ggplot(temp, aes(x = Salinity))
myplot +
geom_histogram(binwidth = 0.5,
color = "lightpink",
fill = "white") +
facet_wrap(~ Month)Warning: Removed 798 rows containing non-finite outside the scale range
(`stat_bin()`).
Temperature by Station
Temperature Boxplots
myplot = ggplot(temp,
aes(x = Station,
y = Temperature))
myplot +
geom_boxplot() +
xlab("Station") +
ylab("Temperature (deg C)") +
ggtitle("Temperature by Station")Warning: Removed 927 rows containing non-finite outside the scale range
(`stat_boxplot()`).
Stations Ordered by Median Temperature
myplot = ggplot(
temp,
aes( x = reorder(Station, Temperature, median),
y = Temperature))
myplot +
geom_boxplot(fill = "lightblue")Warning: Removed 927 rows containing non-finite outside the scale range
(`stat_boxplot()`).
Time Series Analysis
Create a Decimal Date Variable
temp$decdate = temp$Year + temp$dDay3 / 365Salinity Through Time
myplot = ggplot(temp,
aes(x = decdate,
y = Salinity))
myplot +
geom_point(alpha = 0.4,size = 1) +
xlab("Year") +
ylab("Salinity (psu)") +
ggtitle("Salinity Through Time")Warning: Removed 798 rows containing missing values or values outside the scale range
(`geom_point()`).
Salinity Through Time by Area
myplot = ggplot(temp,
aes(x = decdate,
y = Salinity))
myplot +
geom_point(alpha = 0.4,
size = 1) +
facet_wrap(~ Area) +
xlab("Year") +
ylab("Salinity (psu)") +
ggtitle("Salinity Through Time by Area")Warning: Removed 798 rows containing missing values or values outside the scale range
(`geom_point()`).
Salinity Time Series by Station and Area
myplot = ggplot(temp,
aes(x = decdate,
y = Salinity))
myplot +
geom_line(aes(group = Station)) +
facet_wrap(~ Area) +
xlab("Year") +
ylab("Salinity (psu)") +
ggtitle("Salinity Time Series by Station and Area")Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_line()`).