#################################
##                             ##
##   CHAPTER 6: SCATTERPLOTS   ##
##                             ##
#################################

## BIGGER PICTURE 6.1: SCATTERPLOT CORRELATION EXAMPLES ##

par(mfrow=c(2,3)) # arrange following plots in 2 rows with 3 columns
par(mar=c(4.1, 2.1, 2.1, 2.1)) # reduce bottom, left, and top margin sizes

x<-c(seq(2,30)) # list x-values
y<-c(4,5,3,5,6,7,6.5,8,7,8,9,8.5,9.5,10,10.5,11,11.5,11,12,11.5,12,13,
     13.5,13,14,14.5,15,14.75,15.5) # list y-values that would show strong
# positive correlation with x-values
plot(x,y,xaxt="n",yaxt="n",xlab="",ylab="",pch=19) # plot stripped-down scatterplot
mtext("Strong positive\n correlation", # add text label to margin of plot
      side=1, # position at the bottom of the plot
      line=2, # distance from the plotting area
      cex=0.75,font=1) # choose font size and type

y<-c(4,5,3,9,7,6,5,8,7,6,7,8.5,9.5,8,7,11,13,11,10,11.5,10.5,12,13,11.5,
     10,13.5,14,12.5,15) # list y-values that would show moderate
# positive correlation with x-values
plot(x,y,xaxt="n",yaxt="n",xlab="",ylab="",pch=19) # plot stripped-down scatterplot
mtext("Moderate positive\n correlation", # add text label to margin of plot
      side=1,line=2,cex=0.75,font=1)

y<-c(13,5,3,9,10,6,5,8,7,6,7,8.5,9.5,12,7,6,13,9,10,4,8,5,7,11.5,3.5,13.5,
     4,12.5,8) # list y-values that would show 
# no correlation with x-values
plot(x,y,xaxt="n",yaxt="n",xlab="",ylab="",pch=19) # plot stripped-down scatterplot
mtext("No correlation", # add text label to margin of plot
      side=1,line=1,cex=0.75,font=1)

y<-c(15,12.5,14,13.5,10,11.5,13,12,10.5,11.5,10,11,13,11,7,8,9.5,8.5,7,6,
     7,8,5,6,7,9,3,5,4) # list y-values that would show moderate
# negative correlation with x-values
plot(x,y,xaxt="n",yaxt="n",xlab="",ylab="",pch=19) # plot stripped-down scatterplot
mtext("Moderate negative\n correlation", # add text label to margin of plot
      side=1,line=2,cex=0.75,font=1)

y<-c(15.5,14.75,15,14.5,14,13,13.5,13,12,11.5,12,11,11.5,11,10.5,10,9.5,
     8.5,9,8,7,8,6.5,7,6,5,3,5,4) # list y-values that would show strong
# negative correlation with x-values
plot(x,y,xaxt="n",yaxt="n",xlab="",ylab="",pch=19) # plot stripped-down scatterplot
mtext("Strong negative\n correlation", # add text label to margin of plot
      side=1,line=2,cex=0.75,font=1)

y<-c(9,8,7,8,6.5,7,6,5,3,5,4,6,4.5,5,6.5,7,9,8,8.5,10,10.5,9,11,10.5,10,
     11.5,11,12,11) # list y-values that would show 
# non-linear relationship with x-values
plot(x,y,xaxt="n",yaxt="n",xlab="",ylab="",pch=19) # plot stripped-down scatterplot
mtext("Non-linear\n relationship", # add text label to margin of plot
      side=1,line=2,cex=0.75,font=1)

par(mar=c(5.1, 4.1, 4.1, 2.1)) # restore margins to default sizes
par(mfrow=c(1,1)) # return to plotting single plot in plotting area

###########################################################################

##  AN EXAMPLE OF  ##
##   CONTINUOUS    ##
##      DATA       ##

data(Puromycin) # load the built-in data set 'Puromycin' from R
View(Puromycin) # view the data

Puromycin <- read.table(file.choose(), header = T, sep = ",") # OR load the data in from the
                                                              # Excel CSV. file puromycin_R_data.csv
                                                              # and name it 'Puromycin'
View(Puromycin) # view the data

state<-Puromycin$state # assign each variable a simple name
conc<-Puromycin$conc
rate<-Puromycin$rate

###########################################################################

##     LOOKING FOR     ##
##  CORRELATIONS WITH  ##
##     SCATTERPLOTS    ##

### Simple scatterplot

summary(conc) # check min and max concentrations
summary(rate) # check min and max rates

plot(conc,rate, # plot the data: x-axis variable, y-axis variable
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", # set axis limits
     xlab="Concentration (mM)", ylab="Enzymatic reaction rate (DPM/min)") # set axis labels

plot(-5, # create blank plot using value not on axes
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", # set axis limits
     xlab="", ylab="", # keep axis labels blank
     xaxt="n",yaxt="n") # keep tick marks blank
abline(h=(seq(0,250,10)), col="lightgray", lty=1) # set minor horizontal grid lines
abline(h=(seq(0,250,50)), col="darkgray", lty=1) # set major horizontal grid lines
abline(v=(seq(0,1.2,0.05)), col="lightgray", lty=1) # set minor vertical grid lines
abline(v=(seq(0,1.2,0.2)), col="darkgray", lty=1) # set major vertical grid lines
par(new=TRUE) # prepare to add to existing plot
plot(conc,rate, # draw scatterplot of data
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", # set axis limits
     xlab="Concentration (mM)", ylab="Enzymatic reaction rate (DPM/min)", # set axis labels
     col="purple", # choose point outline colour
     pch=24, # choose point type
     bg="yellow", # choose point fill colour
     cex=1.5, # choose point size
     lwd=2) # choose line width of point outline

abline(lm(rate~conc)) # add line of linear model (rate against concentration)


## SCIENTIFIC APPROACH 6.1: MODIFYING POINTS ##

resetPar <- function() {
        dev.new()
        op <- par(no.readonly = TRUE)
        dev.off()
        op
} # create function to reset default par values
pointoptionsfunc<-function(){
        defaultpar<-par()
        par(font=2, mar=c(0.5,0,0,0))
        y=rev(c(rep(1,6),rep(2,5), rep(3,5), rep(4,5), rep(5,5)))
        x=c(rep(1:5,5),6)
        plot(x, y, pch = 0:25, cex=3.5, ylim=c(0.5,6), xlim=c(0.5,6.5), 
             axes=FALSE, xlab="", ylab="", bg="skyblue")
        text(x, y, labels=0:25, pos=3, offset=1.5)
        par(mar=defaultpar$mar,font=defaultpar$font )
} # create function to plot all point types
pointoptionsfunc()
par(resetPar())   
par(mar=c(5.1, 4.1, 4.1, 2.1))
par(oma=c(0,0,0,0)) # restore par defaults


## BIGGER PICTURE 6.3: EXPLAINING LINEAR MODELS ##
lm(rate~conc)
# Rate = 93.92 + 105.4Conc

## SCIENTIFIC APPROACH 6.2: RESTRICTED LINE OF BEST FIT ##
plot(-5, xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="", ylab="",xaxt="n",yaxt="n")
abline(h=(seq(0,250,10)), col="lightgray", lty=1)
abline(h=(seq(0,250,50)), col="darkgray", lty=1)
abline(v=(seq(0,1.2,0.05)), col="lightgray", lty=1)
abline(v=(seq(0,1.2,0.2)), col="darkgray", lty=1)
par(new=TRUE)
plot(conc,rate, xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="Concentration (mM)", ylab="Enzymatic reaction rate (DPM/min)", 
     col="purple", pch=24, bg="yellow", cex=1.5,lwd=2) # plot full figure as above

summary(conc) # check min and max concentrations
x0<-0.02 # set x0 as min concentration
x1<-1.1 # set x1 as max concentration
a<-93.92 # set linear model intercept as a
b<-105.4 # set linear model gradient as b
segments(x0, a+b*x0, x1, a+b*x1, # add restricted line using x0, x1, a and b
         col = "seagreen3", # choose line colour
         lty = 5, # choose line type
         lwd=3) # choose line width


### Scatterplot with multiple samples/data sets

summary(state) # check number of cell states/samples

treated <- subset(Puromycin, state == 'treated') # create 'treated' data set
untreated <- subset(Puromycin, state == 'untreated') # create 'untreated' data set

plot(-5, # create blank plot using value not on axes, as above
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="", ylab="",xaxt="n",yaxt="n")
abline(h=(seq(0,250,10)), col="lightgray", lty=1) # add grid lines as above
abline(h=(seq(0,250,50)), col="darkgray", lty=1)
abline(v=(seq(0,1.2,0.05)), col="lightgray", lty=1)
abline(v=(seq(0,1.2,0.2)), col="darkgray", lty=1)

par(new=TRUE) # prepare to add to existing plot
plot(treated$conc,treated$rate, # plot the 'treated' cells data
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", # set axis limits
     xlab="Concentration (mM)", ylab="Enzymatic reaction rate (DPM/min)", # set axis labels
     pch=15, # choose point type
     col="orange") # choose point colour
par(new=TRUE) # prepare to add to existing plot
plot(untreated$conc,untreated$rate, # plot the 'untreated' cells data
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", # set axis limits
     xlab="", ylab="", xaxt="n",yaxt="n", # do not re-draw axis or tick labels
     pch=19, # choose point type
     col="blue") # choose point colour

abline(lm(treated$rate~treated$conc), # add linear model for 'treated' data
       col="orange", # choose line colour
       lwd=2) # choose line width
abline(lm(untreated$rate~untreated$conc), # add linear model for 'untreated' data
       col="blue", # choose line colour
       lwd=2) # choose line width

legend(0.9,70, # set coordinates for legend placement
       bg="white", # give legend a white background
       pch = c(15,19), # list point types to explain
       col = c("orange", "blue"), # list corresponding colours
       legend=c("Treated","Untreated")) # list corresponding sample names


### Refined scatterplot: adding curved lines of best fit, labelling 
### individual data points, reference lines, and text

plot(-5, xlim=c(0,1.2), ylim=c(0,250), # create blank plot as above
     xaxs = "i",yaxs = "i", xlab="", ylab="",xaxt="n",yaxt="n")
abline(h=(seq(0,250,10)), col="lightgray", lty=1) # add grid lines as above
abline(h=(seq(0,250,50)), col="darkgray", lty=1)
abline(v=(seq(0,1.2,0.05)), col="lightgray", lty=1)
abline(v=(seq(0,1.2,0.2)), col="darkgray", lty=1)
par(new=TRUE) # prepare to add to existing plot
plot(treated$conc,treated$rate, # plot 'treated' data as above
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="Concentration (mM)", ylab="Enzymatic reaction rate (DPM/min)", 
     pch=15, col="orange")
par(new=TRUE) # prepare to add to existing plot
plot(untreated$conc,untreated$rate, # plot 'untreated' data as above
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="", ylab="", xaxt="n",yaxt="n",
     pch=19, col="blue")
legend(0.9,70, bg="white", pch = c(15,19), 
       col = c("orange", "blue"), 
       legend=c("Treated","Untreated")) # add legend as above

treatedcurve<-lowess(treated$conc,treated$rate) # create 'treated' lowess values
untreatedcurve<-lowess(untreated$conc,untreated$rate) # create 'untreated' lowess values
lines(treatedcurve, # add smooth 'treated' curve
      col="orange", # choose line colour
      lwd=2) # choose line width
lines(untreatedcurve, # add smooth 'untreated' curve
      col="blue", # choose line colour
      lwd=2) # choose line width


## SCIENTIFIC APPROACH 6.3: SMOOTHING WITH lowess ##
sub1<-subset(Theoph, Subject == '1') # create subject 1 data set
sub2<-subset(Theoph, Subject == '2') # create subject 2 data set
sub6<-subset(Theoph, Subject == '6') # create subject 6 data set

par(oma=c(1.75,0.5,1.25,0)) # create outer margin space for shared axis labels and legend
par(mar=c(2.5,4,2.5,1)) # edit margin sizes for shared axis labels and legend
par(mfrow=c(3,1)) # arrange following plots in 3 rows with 1 column
plot(sub1$Time,sub1$conc, # plot subject 1 data
     xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", # set axis limits
     ylab="", xlab="", # keep axis labels blank
     pch=15, # choose point type
     col="orange") # choose point colour
par(new=TRUE) # prepare to add to existing plot
plot(sub2$Time,sub2$conc, # plot subject 2 data
     xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", 
     xaxt="n",yaxt="n",ylab="", xlab="",
     pch=16, col="blue")
par(new=TRUE) # prepare to add to existing plot
plot(sub6$Time,sub6$conc, # plot subject 6 data
     xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", 
     xaxt="n",yaxt="n",ylab="", xlab="",
     pch=17, col="green")
mtext("Less smoothing\n(f=0.1)      ", # add text explaining smoothing
      side=3, # position at the top of the plot
      line=-3, # distance from plotting area (- means within the area)
      adj=0.9, # shift alignment to the right
      cex=1) # choose font size
sub1curve0.1<-lowess(sub1$Time,sub1$conc,f=0.1) 
sub2curve0.1<-lowess(sub2$Time,sub2$conc,f=0.1)
sub6curve0.1<-lowess(sub6$Time,sub6$conc,f=0.1) # create lowess values for each subject 
                                                # with little smoothing (f=0.1)
lines(sub1curve0.1,col="orange",lwd=2) 
lines(sub2curve0.1,col="blue",lwd=2)
lines(sub6curve0.1,col="green",lwd=2) # add curved lines for each subject
                                      # using lowess values (f=0.1)

legend("top", # add shared legend to top of plot
       bg="white", # make background white
       bty="n", # do not encase legend with a box
       pch = c(15,16,17), # list point types
       col = c("orange","blue","green"), # list corresponding colours
       cex=1.2, # choose point sizes
       xpd=TRUE, # allow legend to be drawn outside of plotting area
       legend=c("1", "2", "6"), # list corresponding subjects/sample names
       inset=-0.4, # move legend slightly further out from the 'top' of plot
       horiz=TRUE) # arrange legend horizontally

plot(sub1$Time,sub1$conc, xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", 
     ylab="", xlab="",
     pch=15, col="orange")
par(new=TRUE) # prepare to add to existing plot
plot(sub2$Time,sub2$conc, xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", 
     xaxt="n",yaxt="n",ylab="", xlab="",
     pch=16, col="blue")
par(new=TRUE) # prepare to add to existing plot
plot(sub6$Time,sub6$conc, xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", 
     xaxt="n",yaxt="n",ylab="", xlab="",
     pch=17, col="green") # plot all three subjects' data as above
mtext("Default smoothing\n(f=2/3)      ", # add text explaining smoothing
      side=3,line=-3,adj=0.9,cex=1)
sub1curve<-lowess(sub1$Time,sub1$conc)
sub2curve<-lowess(sub2$Time,sub2$conc)
sub6curve<-lowess(sub6$Time,sub6$conc) # create lowess values for each subject 
                                       # with default smoothing (f=2/3)

lines(sub1curve,col="orange",lwd=2)
lines(sub2curve,col="blue",lwd=2)
lines(sub6curve,col="green",lwd=2) # add curved lines for each subject
                                   # using lowess values (f=2/3)
mtext("Theophylline concentration (mg/L)", # add shared y-axis label
      side=2, # position at the left of the plot
      line=3, # distance from plotting area
      cex=1, # choose font size
      xpd=TRUE) # allow shared y-axis label to be drawn outside of plotting area

plot(sub1$Time,sub1$conc, xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", 
     ylab="", xlab="",cex.lab=1.25,xpd=TRUE,
     pch=15, col="orange")
par(new=TRUE) # prepare to add to existing plot
plot(sub2$Time,sub2$conc, xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", 
     xaxt="n",yaxt="n",ylab="", xlab="",
     pch=16, col="blue")
par(new=TRUE) # prepare to add to existing plot
plot(sub6$Time,sub6$conc, xlim=c(-0.5,25), ylim=c(-0.5,12), xaxs = "i",yaxs = "i", 
     xaxt="n",yaxt="n",ylab="", xlab="",
     pch=17, col="green") # plot all three subjects' data as above
mtext("More smoothing\n(f=1.0)      ", # add text explaining smoothing
      side=3,line=-3,adj=0.9,cex=1)
sub1curve1<-lowess(sub1$Time,sub1$conc,f=1)
sub2curve1<-lowess(sub2$Time,sub2$conc,f=1)
sub6curve1<-lowess(sub6$Time,sub6$conc,f=1) # create lowess values for each subject 
                                            # with lots of smoothing (f=1)
lines(sub1curve1,col="orange",lwd=2)
lines(sub2curve1,col="blue",lwd=2)
lines(sub6curve1,col="green",lwd=2) # add curved lines for each subject
                                    # using lowess values (f=1)
mtext("Time since drug administration (hr)", # add shared x-axis label
      side=1, # position at the bottom of the plot
      line=3, # distance from plotting area
      cex=1, # choose font size
      xpd=TRUE) # allow shared x-axis label to be drawn outside of plotting area

par(mfrow=c(1,1)) # return to plotting single plot in plotting area
par(oma=c(0,0,0,0)) # restore default outer margin areas
par(mar=c(5.1, 4.1, 4.1, 2.1)) # restore default margin sizes


### Continuing Refined scatterplot

plot(treated$conc,treated$rate, 
     xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="Concentration (mM)", ylab="Enzymatic reaction rate (DPM/min)", 
     pch=15, col="orange")
par(new=TRUE) # prepare to add to existing plot
plot(untreated$conc,untreated$rate, xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="", ylab="", xaxt="n",yaxt="n",
     pch=19, col="blue")
legend(0.9,70, bg="white", pch = c(15,19), 
       col = c("orange", "blue"), legend=c("Treated","Untreated"))
treatedcurve<-lowess(treated$conc,treated$rate)
untreatedcurve<-lowess(untreated$conc,untreated$rate)
lines(treatedcurve,col="orange",lwd=2)
lines(untreatedcurve,col="blue",lwd=2) # plot figure as above, but
                                       # without grid lines

text(rate~conc, # add text labels at the data points 
     labels=rate, # take the point labels from the reaction rate values 
     pos=2, # position text labels to the left of data points
     cex= 0.8, # choose font size
     font=1) # choose default font face

plot(treated$conc,treated$rate, xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="Concentration (mM)", ylab="Enzymatic reaction rate (DPM/min)", 
     pch=15, col="orange")
par(new=TRUE) # prepare to add to existing plot
plot(untreated$conc,untreated$rate, xlim=c(0,1.2), ylim=c(0,250), xaxs = "i",yaxs = "i", 
     xlab="", ylab="", xaxt="n",yaxt="n",
     pch=19, col="blue")
legend(0.9,70, bg="white", pch = c(15,19), 
       col = c("orange", "blue"), legend=c("Treated","Untreated"))
treatedcurve<-lowess(treated$conc,treated$rate)
untreatedcurve<-lowess(untreated$conc,untreated$rate)
lines(treatedcurve,col="orange",lwd=2)
lines(untreatedcurve,col="blue",lwd=2) # reproduce plot again, as above

interestvals<-subset(Puromycin,conc==0.56) # create data set with reaction rates 
                                           # only at concentration 0.56mM

text(interestvals$rate~interestvals$conc, # add text labels at the data points 
                                          # from our subsetted data
     labels=interestvals$rate, # take the point labels from the reaction rate values
                               # from our subsetted data
     pos=c(4,2,4,2), # position text labels alternately to the right and left
                     # of data points
     cex= 0.8, # choose font size
     font=1) # choose default font face

abline(v=0.56, # add vertical reference line at 0.56mM
       lwd=2, # choose line width
       lty=2, # choose line type
       col="purple") # choose line colour
text(0.65,50, # set coordinates for text placement
     labels="0.56mM", # state text to position
     col="purple", # choose text colour
     font=2) # choose bold font face

text(0.7,220, # set coordinates for text placement
     labels=paste("mean =",mean(c(191,201))), # calculate and print the mean
     col="orange", # choose colour
     font=2) # choose bold font face
text(0.7,125, # set coordinates for text placement
     labels=paste("mean =",mean(c(144,158))), # calculate and print the mean
     col="blue", # choose colour
     font=2) # choose bold font face


###########################################################################

##  PRESENTING  ##
##     TIME     ##
##    SERIES    ##

### Refined time series

temps <- read.table(file.choose(), header = T, sep = ",") # load the data in from the Excel CSV. file 
                                                          # 'land_and_ocean.csv' and name it 'temps'
View(temps) # view the data
summary(temps$year) # check year variable summary
summary(temps$value) # check value variable summary

neg<-subset(temps,value<=0) # create data set of just negative values
pos<-subset(temps,value>0) # create data set of just positive values

plot(400, # create blank plot using value not on axes
     xlim=c(1880,2020), ylim=c(-0.6,1.2), xaxs = "i",yaxs = "i", # set axis limits
     xlab="Year", ylab=expression(paste("Temperature Anomaly ( ",degree,"C)"))) # set axis labels
abline(h=(seq(-0.6,1.2,0.2)), col="lightgray", lty=1) # add minor grid lines
abline(h=0, col="darkgray", lwd=2.5) # add major grid lines

par(new=TRUE) # prepare to add to existing plot
plot(neg$year,neg$value, # plot data set of negative values
     xlim=c(1880,2020), ylim=c(-0.6,1.2),xaxs = "i",yaxs = "i", # set axis limits
     xlab="", ylab="", # do not re-draw axis labels
     xaxt="n",yaxt="n", # do not re-draw axis tick marks
     pch=16, # choose point type
     col="darkblue") # choose point colour
par(new=TRUE) # prepare to add to existing plot
plot(pos$year,pos$value, # plot data set of positive values
     xlim=c(1880,2020), ylim=c(-0.6,1.2), xaxs = "i",yaxs = "i", # set axis limits
     xlab="", ylab="", # do not re-draw axis labels
     xaxt="n",yaxt="n", # do not re-draw axis tick marks
     pch=16, # choose point type
     col="red") # choose point colour

sortedvals <- temps[order(temps$year),] # re-arrange the rows of data by year 
lines(sortedvals$year,sortedvals$value, # add lines connecting ordered data points 
      lty=1, # choose line type
      lwd=1, # choose line width
      col="purple") # choose line colour


### The importance of order in a time series

lute <- read.table(file.choose(), header = T, sep = ",") # load the data in from the 
                                                         # Excel CSV. file 'LH_blood.csv' and
                                                         # name it 'lute'
View(lute) # view the data
summary(lute$time) # check time variable summary
summary(lute$LH)  # check LH variable summary

par(mfrow=c(1,2)) # arrange following plots in 1 row with 2 columns

plot(lute$time,lute$LH, # plot unordered data points
     xlim=c(0,470), ylim=c(1.2,3.6),xaxs = "i",yaxs = "i", 
     xlab="Time (mins)", ylab="LH Concentration (mlU/mL)", 
     pch=17, col="springgreen3")
lines(lute$time,lute$LH, # connect unordered data points
      lty=1,lwd=1.5,col="forestgreen")
mtext("a.", side=3,line=1,adj=-0.2,cex=1.5,font=2) # label panel a

plot(lute$time,lute$LH, # plot unordered data points
     xlim=c(0,470), ylim=c(1.2,3.6),xaxs = "i",yaxs = "i", 
     xlab="Time (mins)", ylab="LH Concentration (mlU/mL)", 
     pch=17, col="springgreen3")
sortedlute <- lute[order(lute$time),] # re-arrange the rows of data by time
lines(sortedlute$time,sortedlute$LH, # connect ordered data points 
      lty=1,lwd=1.5,col="forestgreen")
mtext("b.", side=3,line=1,adj=-0.2,cex=1.5,font=2) # label panel b

par(mfrow=c(1,1)) # return to plotting single plot in plotting area


###########################################################################

##                           ##
##  UNIVARIATE SCATTERPLOTS  ##
##      (STRIP-CHARTS)       ##

flight <- read.table(file.choose(), header = T, sep = ",") # load the data in from the Excel CSV. file 
                                                           # 'birds_and_pterosaurs.csv' and name it 'flight'

# RUN THE FOLLOWING UNTIL YOU HAVE GOOD-LOOKING 'JITTER' ACROSS ALL! #

par(mfrow=c(2,2)) # arrange following plots in 2 rows with 2 columns
par(mar=c(2.1,4.1,2.1,2.1)) # edit margin sizes for all following plots

stripchart(flight$mass_kg ~ flight$broad_group, # plot strip-charts of the data
          ylim=c(-20,280),yaxs="i", # set y-axis limits
          method="jitter", # choose jitter rather than default overplot
          vertical=TRUE, # draw strip-chart vertically
          jitter=0.2, # specify amount of jitter
          ylab = "Mass (kg)", # set y-axis label
          pch=19, # choose point type
          col="darkblue") # choose point colour
mtext("a.", side=3,line=1,adj=-0.2,cex=1,font=2) # label panel a

boxplot(flight$mass_kg ~flight$broad_group, # plot boxplots of the data
        ylim=c(-20,280),yaxs="i", # set axis limits
        ylab = "Mass (kg)", # set y-axis label
        xlab = "", # keep x-axis label blank
        col="yellow") # choose box colour
mtext("b.", side=3,line=1,adj=-0.2,cex=1,font=2) # label panel b

boxplot(flight$mass_kg ~flight$broad_group, # plot boxplots of the data
        ylim=c(-20,280),xlab = "",
        yaxs="i",ylab = "Mass (kg)",col="yellow",
        boxwex=0.3, # set box scaling factor
        at=1.3:2.3) # set where boxes should be drawn along x-axis
stripchart(flight$mass_kg ~ flight$broad_group, # plot strip-charts alongside
           ylim=c(-20,280),
           yaxs="i",method="jitter",jitter=0.1,vertical=TRUE,ylab = "",
           pch=19,col="darkblue",add=TRUE)
mtext("c.", side=3,line=1,adj=-0.2,cex=1,font=2) # label panel c

boxplot(flight$mass_kg ~flight$broad_group, # plot boxplots of the data
        xlab = "",las=1,
        cex.axis=0.75, # reduce font size of axes
        log="y", # draw the y-axis on a logarithmic scale
        ylab = "Mass (kg)",col="yellow",
        boxwex=0.3, # set box scaling factor
        at=1.3:2.3) # set where boxes should be drawn along x-axis
stripchart(flight$mass_kg ~ flight$broad_group, # plot strip-charts alongside
           log="y", # draw the y-axis on a logarithmic scale
           method="jitter",jitter=0.1,vertical=TRUE,ylab = "",
           pch=19,col="darkblue",add=TRUE)
mtext("d.", side=3,line=1,adj=-0.2,cex=1,font=2) # label panel d

par(mfrow=c(1,1)) # return to plotting single plot in plotting area
par(mar=c(5.1,4.1,4.1,2.1)) # restore default margin sizes