# topic 7i: Scatter plots # first generate the data to use in the plot source("../gnrnd4.R") gnrnd4(1273370910, 450008500425) # This generated both L1 and L2. # L1 holds the x values L1 # L2 holds the coresponding y values L2 # then all we need to do is to use the # plot function to get a graph plot( L1, L2 ) # add some horizontal and vertical lines # at the tick marks abline( h=seq(30,55,5), # from 30 to 55 in steps of 5 v=seq(35,55,5), # from 35 to 55 in steps of 5 lty="dotted", # use dotted lines col="blue") # make them blue lines # That is sufficient for the requirements # of this course, but, as usual, it would # be nice to really fix up the plot plot( L1, L2, main="Scatter Plot", # adds a title xlab="x values", # specifies the x axis label ylab="y values", # specifies the y axis label xlim=c(30,60), # sets x to go from 30 to 60 ylim=c(20,60), # sets y to go from 20 to 60 xaxp=c(30,60,15), # sets 15 spaces between x # tick marks from 30 to 60 yaxp=c(20,60,20), # sets 20 spaces between y # tick marks from 20 to 60 las=1, # sets axis values to horizontal pch=19, # sets the points to be # large filled dots col="red") # sets the color of the dots # to be red abline( h=seq(20,60,2), v=seq(30,60,2), col="blue", lty="dotted") abline( h=seq(20,60,10), v=seq(30,60,10), col="darkblue", lty="dashed") # # Here are the EPA values of carbon monoxide levels # measured at various dates at on location (values # are in parts per million) CO <- c( 0.35, 0.636, 0.554, 0.343, 0.842, 0.219, 0.1375, 0.375) # Here are the EPA values for nitrogen dioxide levels # measured at the same location on the same days # (values are in parts per billion) NO2 <- c(6.26, 17.08, 14.85, 8.44, 19.37, 5.78, 3.1, 8.73) # get a scatter plot of that data plot( CO, NO2, main="Data from the EPA", xlab="CO (in ppm)", ylab="NO2 (in ppb)") abline( h=seq(5,20,5), v=seq(0.2, 0.8, 0.1), lty="dashed", col="darkgray") # # Then, we will go back and enter the dates # of the measurements so that we can get a line # chart epa_dates <- as.Date( c( "2019-01-08", "2019-01-16", "2019-01-17", "2019-01-23", "2019-01-27", "2019-01-29", "2019-02-08", "2019-02-22")) # first we can do this as a scatter plot plot( epa_dates, CO) # then fix this up with the x-axis values plot( epa_dates, CO, xaxt="n", xlab="", ylim=c(0,1), yaxp=c(0,1,10), las=1, ylab="Carbon Monoxide (ppm)", main="Data from the EPA") axis.Date(side = 1, dd, format = "%m/%d/%Y", las=2, at=epa_dates, cex.axis=0.6) abline(v=epa_dates, h=seq(0,1,0.1), col="blue", lty="dotted") # then, by using the type= parameter we can get a line # plot plot( epa_dates, CO, type="l", xaxt="n", xlab="", ylim=c(0,1), yaxp=c(0,1,10), las=1, col="red", pch=16, ylab="Carbon Monoxide (ppm)", main="Data from the EPA") axis.Date(side = 1, dd, format = "%m/%d/%Y", las=2, at=epa_dates, cex.axis=0.6) abline(v=epa_dates, h=seq(0,1,0.1), col="blue", lty="dotted") # # but we may want to fix this up with all the # dates on the x axis. And while we are # doing than let us make this plot show # both points and connecting lines plot( epa_dates, CO, type="b", xaxt="n", xlab="", ylim=c(0,1), yaxp=c(0,1,10), las=1, col="red", pch=16, ylab="Carbon Monoxide (ppm)", main="Data from the EPA") axis.Date(side = 1, dd, format = "%m/%d/%Y", las=2, at=seq(as.Date("2019-01-08"), as.Date("2019-02-22"), 1), cex.axis=0.6) abline(v=epa_dates, h=seq(0,1,0.1), col="blue", lty="dotted")