######################################################
#
# UK Beach Waste
#
######################################################

#### GRAEME'S ATTEMPT ####

rm(list = ls()) # It's never a bad idea to clean out R's memory before you start 
                # anything new, and this line does that

debris <- c(46.2,30.4,10.8,8.5,2.9,1.0,0.2) # list values
origins <- c("unknown (46.2%)","public (30.4%)","fishing (10.8%)","sewage (8.5%)",
             "shipping (2.9%)","fly tipping (1.0%)", "medical (0.2%)") # list origins

col_choice <- c("red","pink","orange","yellow",
                "green","blue","grey") # create list of seven colours

pie(debris, # draw a pie chart of the values
    labels = NA, # do not label segments
    clockwise=TRUE, # make segments run clockwise
    col= col_choice) # use list of colours to fill segments
   
legend("bottomright", # position legend
       legend=origins, # use origins list as names
       cex=0.7, # choose font size
       fill=col_choice) # use list of colours


#### ROSALIND'S ATTEMPT ####

percent <- c(46.2,30.4,10.8,8.5,2.9,1.0,0.2) # list values
percentlabels<-paste(percent,"%",sep="") # list values with % symbols
waste <- c("Unknown","Public","Fishing","Sewage-related","Shipping",
           "Fly-tipping", "Medical") # list origins
collist<-c("darkgoldenrod1", "orchid3", "springgreen3","yellow2", "royalblue3",
           "mistyrose","black") # create list of seven colours

## Adding the percentages as labels neatly here is not an easy task.
## This manual method I tried was fun, but not quickly reproducible:
pie(percent, labels = NA, clockwise=TRUE, col= collist)
## Now, try the following code, asking to place the 7 labels:
text(locator(7), percentlabels,cex=0.6)
## The plot window is now interactive, and you can click
## to place the labels and press the 'Finish' button 
## once you've clicked 7 times.

## Adding labels with plot coordinates is also an option
## but this would take a lot of laborious trial-and-error
## for label placement.

## Easy way out:
percent <- c(46.2,30.4,10.8,8.5,2.9,1.0,0.2)
waste <- c("Unknown","Public","Fishing","Sewage-related","Shipping","Fly-tipping",
           "Medical")
collist<-c("darkgoldenrod1", "orchid3", "springgreen3","yellow2", "royalblue3",
           "mistyrose","black")

par(mar=c(0,0,0,5)) # create space to right side of plotting area
pie(percent, # draw a pie chart of the values
    labels = NA, # do not label segments
    clockwise=TRUE, # make segments run clockwise
    col= collist) # use list of colours to fill segments

legend("right", # position legend
       inset=-0.17, # adjust legend relative to plotting area edge
       legend=paste(waste, # use waste origins as legend names
             " (",percent,"%",")", # include the corresponding percentages
                                   # and % symbols enclosed by brackets
             sep=""), # do not leave gaps between these components
       cex=1, # choose default font size
       fill=collist, # use list of colours
       bty="n", # do not encase legend in a box 
       xpd=TRUE) # enable drawing outside plotting area

par(mar=c(5.1, 4.1, 4.1, 2.1)) # return margins to default


######################################################
#
# Attitudes to alcohol
#
######################################################

#### GRAEME'S ATTEMPT ####

rm(list = ls()) # clean R's memory

numbers <- c(62,191,603,512,332) # list values

percent <- round( # create list of rounded relative percentages
    numbers*100/sum(numbers), # calculate relative percentages
    1) # round to one decimal place

choices <- c("Strongly Discouraged","Discouraged","Neither","Encouraged",
             "Strongly Encouraged") # list responses

col_choice <- c("red","pink","orange","palegreen","darkgreen") # list five colours

par(cex=0.7) # change font size for everything to be plotted

pie(percent, # draw pie chart of values
    labels = paste(percent,"%") , # label segments with values and % symbols
    clockwise=TRUE, # make segments run clockwise
    col= col_choice) # use list of colours to fill segments

par(cex=1) # restore default font sizes

legend("topleft", # position legend
       legend=choices, # use list of responses as legend names
       cex=0.57, # choose font size
       fill=col_choice) # list colours for legend


#### ROSALIND'S ATTEMPT ####

frequency <- c(62,191,603,512,332) # list values

percent <- round(frequency*100/sum(frequency),1) # create list of relative percentages
                                                 # rounded to one decimal place

responses <- c("Strongly discouraged","Discouraged","Neither","Encouraged",
               "Strongly encouraged") # list responses

collist <- c("red","pink","gray","cornflowerblue","darkblue") # list five colours

par(mar=c(0,5,0,0)) # create space to left side of plotting area

pie(percent, # draw a pie chart of the values
    labels = paste(percent," %",sep=""), # label segments with values and %
                                         # symbols, with no spaces between components
    clockwise=TRUE, # make segments run clockwise
    col= collist) # use list of colours to fill segments

legend("left", # position legend
       inset=-0.17, # adjust legend relative to plotting area edge
       legend=responses, # use list of responses as legend names
       cex=1, # choose default font size
       fill=collist, # use list of colours
       bty="n", # do not encase legend in a box 
       xpd=TRUE) # enable drawing outside plotting area

par(mar=c(5.1, 4.1, 4.1, 2.1)) # return margins to default
