#######################################
##                                   ##
## CHAPTER 8: ADVANCED CUSTOMISATION ##
##                                   ##
#######################################

###########################################################################

##  INSET A PLOT  ##
##    WITHIN A    ##
##     FIGURE     ##

ghg <- read.table(file.choose(), header = T, sep = ",") # load in the data canada_emissions.csv
View(ghg) # view data

## Bar chart

par(fig = c(0,1,0,1)) # set main plot location in plotting area

canadabar<-barplot(ghg$emissions~ghg$year, # plot the data
                   cex.names=0.8, # set size of x-axis names
                   ylim=c(19,23),yaxs = "i", # set y-axis limits
                   xaxt="n", # do not draw x-axis
                   ylab=expression(GHG~Capita~(t~CO[2]~eq/capita)), # set y-axis label
                   xlab="", # keep x-axis label blank
                   col="seagreen1", # choose colour
                   xpd=FALSE) # do not extend bars outside plotting area
abline(h=19) # draw clear y-axis baseline (x-axis)
axis(side=1, # draw axis on bottom of plot
     at = canadabar, # add ticks in the middle of the bars
     labels=ghg$year, # label ticks with the years
     las=1) # orient tick labels horizontally

par(fig = c(0.4,1, 0.35, 1), # set inset plot location in plotting area
    new = TRUE)  # prepare to draw new plot on existing plotting area

fullbar<-barplot(ghg$emissions~ghg$year, # plot the data
                 cex.axis=0.7, # set size of y-axis tick labels
                 ylim=c(0,25),yaxs = "i", # set y-axis limits
                 ylab="", xlab="", # keep axes labels blank
                 xaxt="n", # do not draw x-axis
                 col="seagreen1") # choose colour
abline(h=0) # draw clear y-axis baseline (x-axis)
axis(side=1, # draw axis on bottom of plot
     at = fullbar, # add ticks in the middle of the bars
     labels=ghg$year, # label ticks with the years
     las=2, # orient tick labels perpendicular to the axis
     cex.axis=0.7, # set axis tick label sizes
     tck=-0.05, # set tick mark length
     hadj=0.6) # adjust tick label positioning

## SCIENTIFIC APPROACH 8.1: CONSTRUCTING ##
##       EXPRESSION COMMANDS IN R        ##

par(fig = c(0,1,0,1))
plot(1,1,xaxt="n",yaxt="n",xlab="",ylab="",col="white")

text(1,1.3, "Using expression and paste", col="firebrick3",font=2)

text(1,1.2,expression(paste("Subscript"[subscript]," ","Superscript"^superscript,
                            "     ","plain text"," ",gamma)),
     col="firebrick3")

text(1,0.8, "Using expression alone", col="darkblue",font=2)

text(1,0.7,expression(Subscript[subscript]~Superscript^superscript~~~~~plain~text~gamma),
     col="darkblue")

##  Time series (scatterplot)

par(fig = c(0,1,0,1)) # set main plot location in plotting area

plot(ghg$emissions~ghg$year, # plot the data
     xlim=c(2005,2017), xaxs = "i", # set x-axis limits
     ylim=c(19,23), yaxs = "i", # set y-axis limits
     xlab="Year", # set x-axis label
     ylab=expression(GHG~Capita~(t~CO[2]~eq/capita)), # set y-axis label
     pch=16, # choose point type
     col="seagreen1", # choose colour
     xaxt="n") # do not draw x-axis
axis(side=1, # draw axis on bottom of plot
     at = seq(2005,2017,2), # set values for ticks to be drawn at
     labels=ghg$year, # label ticks with the years
     las=1) # orient tick labels horizontally
sortedvals <- ghg[order(ghg$year),] # order emissions data by year
lines(sortedvals$year,sortedvals$emissions, # connect sorted data with lines
      lty=1, # choose line type
      lwd=2, # choose line width
      col="forestgreen") # choose line colour

par(fig = c(0.4,0.95, 0.35, 0.95), # set inset plot location in plotting area
    new = TRUE)   # prepare to draw new plot on existing plotting area
plot(ghg$emissions~ghg$year, # plot the data
     cex.axis=0.7, # set size of y-axis tick labels
     xlim=c(2005,2017),xaxs = "i", # set x-axis limits
     ylim=c(0,25), yaxs = "i", # set y-axis limits
     xlab="", ylab="", # keep axes labels blank
     pch=16, # choose point type
     col="seagreen1", # choose colour
     xaxt="n") # do not draw x-axis
axis(side=1, # draw axis on bottom of plot
     at = seq(2005,2017,2), # set values for ticks to be drawn at
     labels=ghg$year, # label ticks with the years
     las=2, # orient tick labels perpendicular to the axis
     cex.axis=0.7, # set axis tick label sizes
     tck=-0.04, # set tick mark length
     hadj=0.6) # adjust tick label positioning
lines(sortedvals$year,sortedvals$emissions, # connect sorted data with lines
      lty=1, # choose line type
      lwd=2, # choose line width
      col="forestgreen") # choose line colour

###########################################################################

##     ADDING AND     ##
##  REMOVING FRAMES   ##
##     FROM PLOTS     ##

par(fig = c(0,1,0,1)) # produce bar chart from above
canadabar<-barplot(ghg$emissions~ghg$year, cex.names=0.8,
                   ylim=c(19,23),yaxs = "i",xaxt="n",
                   ylab=expression(GHG~Capita~(t~CO[2]~eq/capita)), xlab="", 
                   col="seagreen1",xpd=FALSE)
abline(h=19)
axis(side=1, at = canadabar, labels=ghg$year,las=1)

par(fig = c(0.4,0.95, 0.35, 0.95), # set different inset plot location in plotting area
    new = TRUE)  
fullbar<-barplot(ghg$emissions, # produce inset bar chart from above
                 cex.axis=0.7,
                 ylim=c(0,25),yaxs = "i",
                 ylab="", xlab="", xaxt="n",
                 col="seagreen1")
abline(h=0)
axis(side=1, at = fullbar, labels=ghg$year,las=2,cex.axis=0.7,tck=-0.05,hadj=0.6)

## Adding frames

par(fig = c(0.4,0.95, 0.35, 0.95)) # refer to the inset plot location
box(which = "plot", # add a frame around the inset plot
    lty = 1) # choose default line type for frame

par(fig = c(0,1, 0, 1)) # refer to the main plot location
box(which = "plot", # add a frame around the main plot
    lty = 1) # choose default line type for frame

par(fig = c(0,1,0,1)) # refer to the whole plotting area
box(which = "figure", # add a frame around the entire figure
    lty = 2, # choose line type for frame
    lwd=3, # choose line width for frame
    col="blue") # choose colour for frame

## Removing default frames

par(fig = c(0,1,0,1))
plot(ghg$emissions~ghg$year, # produce main time series plot from above
     xlim=c(2005,2017), xaxs = "i",
     ylim=c(19,23), yaxs = "i", 
     xlab="Year", ylab=expression(GHG~Capita~(t~CO[2]~eq/capita)),
     pch=16, col="seagreen1",
     xaxt="n",
     frame=FALSE) # this draws the scatterplot without the default frame
axis(side=1, at = seq(2005,2017,2), labels=ghg$year,las=1)
sortedvals <- ghg[order(ghg$year),]
lines(sortedvals$year,sortedvals$emissions,lty=1,lwd=2,col="forestgreen")

par(fig = c(0.4,0.95, 0.35, 0.95), 
    new = TRUE)  
plot(ghg$emissions~ghg$year, # produce inset time series plot from above
     cex.axis=0.7,
     xlim=c(2005,2017), xaxs = "i",
     ylim=c(0,25), yaxs = "i", 
     xlab="", ylab="",
     pch=16, col="seagreen1",
     xaxt="n",
     frame=FALSE) # this draws the scatterplot without the default frame
axis(side=1, at = seq(2005,2017,2), labels=ghg$year,
     las=2,cex.axis=0.7,tck=-0.04,hadj=0.6)
lines(sortedvals$year,sortedvals$emissions,lty=1,lwd=2,col="forestgreen")


par(fig = c(0,1,0,1)) # set main plot location in plotting area
boxplot(ghg$emissions, # draw a boxplot of just the emissions data
        ylim=c(18,24),yaxs = "i", # set y-axis limits
        ylab=expression(GHG~Capita~(t~CO[2]~eq/capita)), # set y-axis label
        col="seagreen1", # chosoe colour
        frame=FALSE) # this draws the boxplot without the default frame

###########################################################################

##    ADDING IMAGES    ##
##         AND         ##
##  BACKGROUND IMAGES  ##

## Adding images

install.packages("png") # install package to load in PNG files
library(png) # activate package

beef<-readPNG(file.choose()) # load in cow.png
pork<-readPNG(file.choose()) # load in pig.png
chicken<-readPNG(file.choose()) # load in chicken.png

par(fig = c(0,1,0,1))
canadabar<-barplot(ghg$emissions~ghg$year, # re-plot main bar chart
                   cex.names=0.8,
                   ylim=c(19,23),yaxs = "i",xaxt="n",
                   ylab=expression(GHG~Capita~(t~CO[2]~eq/capita)), xlab="", 
                   col="seagreen1",
                   xpd=FALSE)
abline(h=19)
axis(side=1, at = canadabar, labels=ghg$year,las=1)

rasterImage(beef, # add 'beef' image to main plot
            canadabar[1]-0.48, # choose 1st bar image x1 coordinate for 'beef'
            19.05, # choose y1 coordinate for 'beef'
            canadabar[1]+0.42, # choose 1st bar image x2 coordinate for 'beef'
            19.7) # choose y2 coordinate for 'beef'
rasterImage(beef,
            canadabar[2]-0.48,19.05, # choose 2nd bar image coordinates x1, y1
            canadabar[2]+0.42,19.7) # choose 2nd bar image coordinates x2, y2
rasterImage(chicken, # add 'chicken' image to main plot
            canadabar[3]-0.3, # choose 3rd bar image x1 coordinate for 'chicken'
            19.05, # choose y1 coordinate for 'chicken'
            canadabar[3]+0.3, # choose 3rd bar image x2 coordinate for 'chicken'
            19.4) # choose y2 coordinate for 'chicken'
rasterImage(pork, # add 'pork' image to main plot
            canadabar[4]-0.4, # choose 4th bar image x1 coordinate for 'pork'
            19.05, # choose y1 coordinate for 'pork'
            canadabar[4]+0.4, # choose 4th bar image x2 coordinate for 'pork'
            19.5) # choose y1 coordinate for 'pork'
rasterImage(pork,
            canadabar[5]-0.4,19.05, # choose 5th bar image coordinates x1, y1
            canadabar[5]+0.4,19.5) # choose 5th bar image coordinates x2, y2
rasterImage(chicken,
            canadabar[6]-0.3,19.05, # choose 6th bar image coordinates x1, y1
            canadabar[6]+0.3,19.4) # choose 6th bar image coordinates x2, y2
rasterImage(chicken,
            canadabar[7]-0.3,19.05, # choose 7th bar image coordinates x1, y1
            canadabar[7]+0.3,19.4) # choose 7th bar image coordinates x2, y2

par(fig = c(0.4,0.95, 0.35, 0.95), new = TRUE)  
fullbar<-barplot(ghg$emissions, # re-plot inset bar chart
                 cex.axis=0.7,
                 ylim=c(0,25),yaxs = "i",
                 ylab="", xlab="", xaxt="n",
                 col="seagreen1")
abline(h=0)
axis(side=1, at = fullbar, labels=ghg$year,las=2,cex.axis=0.7,tck=-0.05,hadj=0.6)

rasterImage(beef, # add 'beef' image to inset plot
            fullbar[1]-0.48, # choose 1st bar image x1 coordinate for 'beef'
            0.05, # choose y1 coordinate for 'beef'
            fullbar[1]+0.42, # choose 1st bar image x2 coordinate for 'beef'
            4.7) # choose y2 coordinate for 'beef'
rasterImage(beef,
            fullbar[2]-0.48,0.05,
            fullbar[2]+0.42,4.7) # choose coordinates (x1, y1, x2, y2)
rasterImage(chicken, # add 'chicken' image to inset plot
            fullbar[3]-0.3, # choose 3rd bar image x1 coordinate for 'chicken'
            0.05, # choose y1 coordinate for 'chicken'
            fullbar[3]+0.3, # choose 3rd bar image x2 coordinate for 'chicken'
            4.4) # choose y2 coordinate for 'chicken'
rasterImage(pork, # add 'pork' image to inset plot
            fullbar[4]-0.4, # choose 4th bar image x1 coordinate for 'pork'
            0.05, # choose y1 coordinate for 'pork'
            fullbar[4]+0.4, # choose 4th bar image x2 coordinate for 'pork'
            4.6) # choose y2 coordinate for 'pork'
rasterImage(pork,
            fullbar[5]-0.4,0.05,
            fullbar[5]+0.4,4.6) # choose coordinates (x1, y1, x2, y2)
rasterImage(chicken,
            fullbar[6]-0.3,0.05,
            fullbar[6]+0.3,4.4) # choose coordinates (x1, y1, x2, y2)
rasterImage(chicken,
            fullbar[7]-0.3,0.05,
            fullbar[7]+0.3,4.4) # choose coordinates (x1, y1, x2, y2)

## Add frames back on

par(fig = c(0.4,0.95, 0.35, 0.95)) # refer to the inset plot location
box(which = "plot", # add a frame around the inset plot
    lty = 1) # choose default line type for frame

par(fig = c(0,1, 0, 1)) # refer to the main plot location
box(which = "plot", # add a frame around the main plot
    lty = 1) # choose default line type for frame

par(fig = c(0,1,0,1)) # refer to the whole plotting area
box(which = "figure", # add a frame around the entire figure
    lty = 2, # choose line type for frame
    lwd=3, # choose line width for frame
    col="blue") # choose colour for frame

## Below is our attempt at figuring out image coordinates (without
## the help of axes values) using trial and error. These could have
## been added at any stage in the plotting process using par(fig).
par(fig = c(0,1,0,1)) # add images to the main plot 
rasterImage(beef,-1.1,-7.8,0,-2.8)
rasterImage(beef,0.4,-7.8,1.5,-2.8)
rasterImage(chicken,2.2,-7.8,2.7,-5)
rasterImage(pork,3.4,-7.8,4.5,-4)
rasterImage(pork,4.9,-7.8,6,-4)
rasterImage(chicken,6.7,-7.8,7.2,-5)
rasterImage(chicken,8.1,-7.8,8.6,-5)
par(fig = c(0.4,0.95, 0.35, 0.95)) # add images to inset plot
rasterImage(beef,0.2,0.3,1.1,4.6)
rasterImage(beef,1.4,0.3,2.3,4.6)
rasterImage(chicken,2.8,0.3,3.3,4.6)
rasterImage(pork,3.8,0.3,4.7,4.6)
rasterImage(pork,5.1,0.3,6,4.6)
rasterImage(chicken,6.4,0.3,6.9,4.6)
rasterImage(chicken,7.6,0.3,8.1,4.6)
## BUT the beauty of R is that you can re-run things easily by 
## copying and pasting code, so we think producing each plot
## separately and adding images in order (as we did earlier) makes 
## the most sense.


## Adding a background image:
#install.packages("jpeg") # install package to load in JPEG files
library(jpeg) # activate package

canadaflag <- readJPEG(file.choose()) # load in flagCAN.jpg

par(fig = c(0,1, 0, 1)) # set plot area to fill whole plotting area
plot(1:2, # produce a plot of arbitrary values
     type='n', # draw a blank plot
     xaxt="n", yaxt="n", # do not draw axes
     xlab="", ylab="") # keep axes labels blank

lim <- par() #extract plot limits
rasterImage(canadaflag, # add 'canadaflag' image to plotting area
            lim$usr[1], lim$usr[3], 
            lim$usr[2], lim$usr[4]) # choose coordinates (x1, y1, x2, y2)
                                    # based on extracted limits

par(fig = c(0,1,0,1), # set main plot location in plotting area
    new=TRUE) # prepare to draw new plot on existing plotting area
canadabar<-barplot(ghg$emissions~ghg$year, # plot main bar chart
                   cex.names=0.8,
                   ylim=c(19,23),yaxs = "i",xaxt="n",
                   ylab=expression(GHG~Capita~(t~CO[2]~eq/capita)), xlab="", 
                   col="seagreen1",
                   xpd=FALSE)
abline(h=19)
axis(side=1, at = canadabar, labels=ghg$year,las=1)
rasterImage(beef, # add images on to main bar chart 'canadabar'
            canadabar[1]-0.48, 19.05, canadabar[1]+0.42, 19.7) 
rasterImage(beef, canadabar[2]-0.48, 19.05, canadabar[2]+0.42, 19.7) 
rasterImage(chicken, canadabar[3]-0.3, 19.05, canadabar[3]+0.3, 19.4) 
rasterImage(pork, canadabar[4]-0.4, 19.05, canadabar[4]+0.4, 19.5) 
rasterImage(pork, canadabar[5]-0.4, 19.05, canadabar[5]+0.4, 19.5) 
rasterImage(chicken, canadabar[6]-0.3, 19.05, canadabar[6]+0.3, 19.4) 
rasterImage(chicken, canadabar[7]-0.3, 19.05, canadabar[7]+0.3, 19.4)

par(fig = c(0.4,0.95, 0.35, 0.95), # set inset plot location in plotting area
    new = TRUE) # prepare to draw new plot on existing plotting area
fullbar<-barplot(ghg$emissions, # plot inset bar chart
                 cex.axis=0.7,
                 ylim=c(0,25),yaxs = "i",
                 ylab="", xlab="", xaxt="n",
                 col="seagreen1")
abline(h=0)
axis(side=1, at = fullbar, labels=ghg$year,las=2,cex.axis=0.7,tck=-0.05,hadj=0.6)
rasterImage(beef, # add images on to inset bar chart 'fullbar'
            fullbar[1]-0.48, 0.05, fullbar[1]+0.42, 4.7) 
rasterImage(beef, fullbar[2]-0.48, 0.05, fullbar[2]+0.42, 4.7) 
rasterImage(chicken, fullbar[3]-0.3, 0.05, fullbar[3]+0.3, 4.4) 
rasterImage(pork, fullbar[4]-0.4, 0.05, fullbar[4]+0.4, 4.6) 
rasterImage(pork, fullbar[5]-0.4, 0.05, fullbar[5]+0.4, 4.6) 
rasterImage(chicken, fullbar[6]-0.3, 0.05, fullbar[6]+0.3, 4.4) 
rasterImage(chicken, fullbar[7]-0.3, 0.05, fullbar[7]+0.3, 4.4)


par(fig = c(0.4,0.95, 0.35, 0.95))  
box(which = "plot", lty = 1) # add frame to inset plot
par(fig = c(0,1, 0, 1))  
box(which = "plot", lty = 1) # add frame to main plot
par(fig = c(0,1,0,1))  
box(which = "figure", lty = 2, lwd=3,col="blue") # add frame around entire figure


###########################################################################

##                    ##
##  LOGARITHMIC AXES  ##
##                    ##

x <- seq(0, 100, 1) # generate x-values
y <- expm1(x) # generate exponential y-values

plot(x, y) # visualise x and y as scatterplot

plot(x, y, # plot x and y as a scatterplot
     log="xy") # plot with logarithmic x and y axes

plot(x, y, # plot x and y as a scatterplot
     log="xy", # plot with logarithmic x and y axes
     xaxt="n",yaxt="n", # do not draw axes
     pch=20, # choose point type
     col="darkmagenta") # choose point colour
axis(side=1, # draw axis on bottom of plot
     at=c(1,10,50,100), # set values for ticks to be drawn at
     tck=-0.04) # set tick mark length
minorx<-c(rep("",3)) # create list of blank values for minor tick labels
axis(side=1, # draw axis on bottom of plot
     at=c(5,25,75), # set values for minor ticks to be drawn at
     labels=minorx, # label minor ticks with list of blank values
     tck=-0.02) # set minor tick mark length
ymajor <-c(0.1,100,expression(10^10),
           expression(10^25), expression(10^45)) # create list of y-axis
                                                 # major tick labels
axis(side=2, # draw axis on left of plot
     at=c(0.1,100,10^10,10^25,10^45), # set values for ticks to be drawn at
     labels=ymajor, # label major ticks using list
     las=2) # orient major tick labels perpendicular to the axis

lines(x,y, # add a line through the data points
      lty=1, # choose line type
      lwd=1, # choose line width
      col="darkmagenta") # choose line colour


###########################################################################

##  ADDING ARROWS,  ##
##   SYMBOLS AND    ##
##     SHAPES       ##

## Add arrow
arrows(2.5,10^9,3.14159,30, # set coordinates, (x0,y0,x1,y1)
       lwd=2, # choose line width
       col="red", # choose colour
       length=.1) # set size for arrowhead

## Add Greek symbols
text(2.5,10^11, # set coordinates
     labels=expression(paste("x = ", pi)), # set text
     cex = 1.5) # set font size

## Add arrow and mathematical symbols
arrows(50,10^30,70,10^44, 
       lwd=3,
       lty=2,
       col="forestgreen",
       length=.2)
text(45,10^28,
     labels=expression(infinity), # print mathematical symbol
     cex=2.5)

## Add superscripts and subscripts
text(5,10^40, 
     labels=expression(paste("(Zn[OH]"[4],")"^{"2-"} %<->% "ZnO + H"[2],"O + 2OH"^{"-"})),cex=1.2)

## Drawing polygons (for shading)
polygon(c(1, x, max(x)), # set min(x), x, max(x) for placement
        c(1.718282, y, 1.718282), # set min(y), y, min(y) for placement 
        col="chartreuse") # choose shading colour

## Shade restricted area
xshade <- seq(10,50,1) # list range of x-values to shade
yshade <- y[c(seq(11,51,1))] # list range of y-values to shade
polygon(c(min(xshade), xshade, max(xshade)), # set min(x), x, max(x) for placement
        c(1.718282, yshade, 1.718282), # set min(y), y, min(y) for placement
        col="chartreuse4") # choose shading colour

## Drawing polygons (randomly)
shapex <- c(8,3,6,10,15,20,25) # list x-coordinates
shapey <- c(10^15,10^17,10^30,10^25,10^35,10^30,10^20) # list y-coordinates
polygon(shapex, shapey, # draw polygon at listed coordinates
        col="pink", # choose fill colour
        border="royalblue", # choose border colour
        lwd=4) # choose line width of border

## Drawing circles (transparent)
install.packages("plotrix") # install 'plotrix' package
library(plotrix) # activate package
par(new=TRUE) # prepare to draw new plot on existing plotting area
plot(c(seq(0,100,1)), # plot values that will establish x-coordinates 1-100
     c(seq(0,100,1)), # plot values that will establish y-coordinates 1-100
     type='n', # draw a blank plot
     xaxt="n", yaxt="n", # do not draw axes
     xlab="", ylab="") # keep axes labels blank

install.packages("yarrr") # install 'yarrr' package
library(yarrr) # activate package

draw.circle(82.5,63.5, # set coordiantes at which to draw a circle
            radius=4, # set radius size
            nv=1000, # choose number of vertices to draw with
            border="black", # choose border colour
            col=transparent(orig.col = "goldenrod1", # choose fill colour
                            trans.val = 1), # choose how transparent to make it
            lty=1, # choose border line type
            lwd=2) # choose border line width

## Drawing circle (filled)
draw.circle(15,65, # set coordiantes at which to draw a circle
            radius=8, # set radius size
            nv=1000, # choose number of vertices to draw with
            border="orange", # choose border colour
            col="seagreen1", # choose fill colour
            lty=3, # choose border line type
            lwd=3, # choose border line width
            density=50, # choose density of shading lines
            angle=45) # choose angle of pattern filling the shape

###########################################################################

##  ANNOTATING IMAGES  ##
##   AND GEOGRAPHIC    ##
##         MAPS        ##

## Annotating images
install.packages("png") # install package to load in PNG files
library(png) # activate package

experiment <- readPNG(file.choose()) # load in image experiment.png

par(fig = c(0,1, 0, 1)) # set plot area to fill whole plotting area
plot(c(seq(0,100,1)), # plot values that will establish x-coordinates 1-100
     c(seq(0,100,1)), # plot values that will establish y-coordinates 1-100
     type='n', # draw a blank plot
     xaxt="n", yaxt="n", # do not draw axes
     xlab="", ylab="") # keep axes labels blank

lim <- par() # extract plot limits
rasterImage(experiment, # add 'experiment' image to plotting area
            lim$usr[1], lim$usr[3], 
            lim$usr[2], lim$usr[4]) # choose coordinates (x1, y1, x2, y2)
box(which = "plot", lty = 1) # add frame around plotting area

arrows(78,12,55,38, lwd=2,col="skyblue",length=.1) # add an arrow to the aphid
text(89,13,labels="Pea aphid", # add text label for the aphid
     col="skyblue",font=2,cex=1) 
text(89,6,
     labels=expression("("*italic(A.~pisum)*")"), # add species name in italics
     col="skyblue",cex=0.8)  
arrows(83,88,55,44, lwd=2,col="red",length=.1) # add an arrow to the ladybird
text(93,92,labels="2-spot\n Ladybird", # add text label for the ladybird
                                       # spanning two lines
     col="red",font=2,cex=1)  
text(93,82,labels=expression("("*italic(A.~bipunctata)*")"), # add species name in italics
     col="red",cex=0.8)  
segments(15, 52, 30, 99,col="goldenrod1",lwd=3) # add lines to bracket the pot
segments(15, 52, 30, 1,col="goldenrod1",lwd=3)
segments(13, 52, 15,52,col="goldenrod1",lwd=3)  
text(7,52,labels="Bean\nseedlings", # add text label for the seedlings
     col="goldenrod1",font=2,cex=1)  
text(7,40,labels=expression("("*italic(V.~faba)*")"), # add species name in italics
     col="goldenrod1",cex=0.8)  
text(7,3,labels="Trial 13", # add text label for the experimental trial number
     col="white",font=2,cex=1) 


## Annotating maps
library(png) # activate package
rivermap <- readPNG(file.choose()) # load in image river_map.png

par(fig = c(0,1, 0, 1)) # set plot area to fill whole plotting area
plot(c(seq(0,100,1)), # plot values that will establish x-coordinates 1-100
     c(seq(0,100,1)), # plot values that will establish y-coordinates 1-100
     type='n', # draw a blank plot
     xaxt="n", yaxt="n", # do not draw axes
     xlab="", ylab="") # keep axes labels blank

lim <- par() # extract plot limits
rasterImage(rivermap, # add 'rivermap' image to plotting area
            lim$usr[1], lim$usr[3], 
            lim$usr[2], lim$usr[4]) # choose coordinates (x1, y1, x2, y2)
box(which = "plot", lty = 1) # add frame around plotting area

segments(55, 85, 75, 85,col="goldenrod1",lwd=3) # add lines to construct scale bar
segments(55, 85, 55, 82,col="goldenrod1",lwd=3)  
segments(75, 85, 75, 82,col="goldenrod1",lwd=3)  
text(65,93,"200m", # add text label for scale bar
     col="goldenrod1",font=2,cex=1.75)  

segments(12, 45, 17, 37,col="skyblue",lwd=5,lty=1) # add lines to show cross-section
                                                   # locations at meanders
segments(21, 85, 23, 74,col="skyblue",lwd=5,lty=1) 
segments(58, 2, 62, 11,col="skyblue",lwd=5,lty=1) 
text(20,47,labels="A", # add text labels at each study site
     col="skyblue",font=2,cex=1.5) 
text(27,87,labels="B",col="skyblue",font=2,cex=1.5) 
text(55,11,labels="C",col="skyblue",font=2,cex=1.5) 

library(plotrix) # activate package 'plotrix'
draw.circle(8,49,radius=2,nv=1000,border="orange",col="red",lwd=1) # draw circles to represent
                                                                   # pebble sizes
draw.circle(20,92,radius=1,nv=1000,border="orange",col="red",lwd=1)
draw.circle(66,17,radius=2.5,nv=1000,border="orange",col="red",lwd=1)


