giss = read.csv("../data/giss_temp.csv")Module 6 Exercise 1
Introduction
Referencing the previous exercise from module 5, I will be using the giss_temp.csv file. This file contains global temperature anomalies from the years 1881 to 2011.
Loading Data
The code above loads in the file I want to use for the following exercises. It creates a dataset in my environment to make it easy to reference.
Part 1
In part 1 of this exercise, I had to create a loop to plot select time series across all years and include the months for each year. Essentially, each plot is a single year from 1881 to 2011 but contains all 12 month. This plotted the temperature anomalies for each month in that year.
Loop Across All Years Using for() Loops
allyears = unique(giss$Year)
for (i in 1:length(allyears)) {
yearID = which(giss$Year == allyears[i])
png(paste("giss_year_", allyears[i], ".png", sep=''))
plot(giss$Month[yearID], giss$TempAnom[yearID],
xlab = "Month", ylab = "Temperature Anomaly",
main = paste("Monthly Temp. Anomaly -", allyears[i]),
ylim = range(giss$TempAnom))
dev.off()
}I will break up the above code line by line:
- The first line breaks up the years into a vector. Using just
giss$Yearwould cause years to repeat, sounique()individualizes them. - The second line starts a
for()loop that looks over each individual year. The loop only runs once for each year in the data. - The third line creates a vector called
yearIDthat finds which rows in the data belong to which individual year. - The fourth line creates an image file for each plot that is generated as a png.
- The fifth line actually creates the graph, showing the temperature anomalies over the months during each year. Then, to make the plots neater,
xlabandylablabel each axis. The title is created usingmain, and the y-axis is kept the same across each plot usingylim. - The sixth line saves each plot and then goes on to the next one.
This piece of code generates 130 plots, loading them into the directory you set.
Part 2
In part 2 of this exercise, I had to create a plot with vertical lines to look at the temperature anomalies for each year from 1881 to 2011. Specifically, I was looking at changing the colors of the vertical lines before and after the year 1980. The temperature anomalies before 1980 are light blue and the temperature anomalies after 1980 are pink.
Changing Temperature Anomaly Colors Using ifelse() Statements
allyears = unique(giss$Year)
ann_temp = tapply(giss$TempAnom, giss$Year, mean)
mycols = ifelse(allyears < 1980, "lightblue", "pink")
plot(allyears, ann_temp, type = 'h',
col = mycols, lwd = 3,
xlab = "Year", ylab = "Temperature Anomaly",
main = "Annual Temp Anomaly by Year")I will break up the above code line by line:
- The first line breaks up the years into a vector. Using just
giss$Yearwould cause years to repeat, sounique()individualizes them. - The second line uses
tapply()to group the temperature anomalies by year while taking the average temperature anomaly for each year. - The third line creates a vector of colors using the
ifelse()function to distinguish between the years before and after 1980. I set the years before 1980 to be light blue, and to change to pink if it does not meet those requirements (any year after 1980). - The fourth line contains the actual plot of the temperature anomalies by year, and the
type = 'h'indicates vertical lines rather than a traditional plot. The other arguments set the colorcol(), line widthlwd(), axis labelsxlab()andylab(), and titlemain().