# CVEN 5454 - Lecture 2, 01/16/2020 # Part II # # Next we'll go over a more complicated example of using boxplot and # histogram functions ################################################################################ # First we will download the data from the CVEN 5454 website: rain <- read.table("http://civil.colorado.edu/~balajir/r-session-files/aismr.txt") names(rain) <- c('Year', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') # name the columns # we just want the rainfall data not the year column rain_months <- rain rain_months$Year = NULL # delete the year column # use the boxplot function to plot many boxplots on 1 graph! boxplot(x = rain_months, xlab="Months", ylab="Monthly rainfall in India (mm)") # let's add the mean values for every month # find the means of each month # (the apply function lets you apply a statistic to each column or row of a # dataframe) month_means <- apply(X=rain_months, MARGIN=2, FUN=mean) #MARGIN is the dimension # add points to our existing boxplot points(month_means, col='blue', pch=16) # add lines to connect if we feel like it lines(month_means,col='blue',lwd=4) ################################################################################ # Now let's do a more complex histogram example # Plot histogram of each month and comment on the features.. # command for pre-allocating your graphing spaces par(mfrow=c(3,4)) # 3,4 means we want 3 rows by 4 columns of histograms (12 months) # define month names for titling months <- c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") # plot 12 histograms (1 for each month) in a for loop for(i in 1:12){ hist(rain_months[,i], xlab="Monthly rainfall", ylab="", freq=FALSE, main="") title(main=c(months[i],' rainfall')) box() # adds a nice box }