######################################
##                                  ##
## CHAPTER 2: PIE CHARTS AND TABLES ##
##                                  ##
######################################

##              ##
##  PIE CHARTS  ##
##              ##

### Nominal data (unordered categories)

#### Simple pie chart

germs<-c(27,75,23,48,26) # create a list of the values

surfaces<-c("Computer keyboards","Sink handles",
            "Water fountain buttons","Microwave door handles",
            "Refrigerator door handles") # create a list of labels for the surfaces

pie(germs, # draw a pie chart of the values in the list 'germs'
    labels=surfaces) # use the list 'surfaces' to label the segments

surfaces<-c("Sink handles","Microwave door handles",
            "Computer keyboards","Refrigerator door handles",
            "Water fountain buttons") # list surface labels in order of
                                      # descending corresponding value

germs<-c(75,48,27,26,23) # arrange the values in descending order
                         # EITHER by re-typing as a list

germs<-sort(germs, # OR by using the sort function on
                   # the existing list 'germs'
            decreasing=TRUE) # and sorting it by decreasing value

pie(germs, # draw a pie chart of the values in the reordered list 'germs'
    labels=surfaces, # use the reordered list 'surfaces' to label the segments
    clockwise=TRUE) # make segments run clockwise from 12 'o' clock position


## BIGGER PICTURE 2.1: QUICKLY REARRANGE VALUES AND LABELS ##

germs<-c(27,75,23,48,26) # start with unordered list of values
surfaces<-c("Computer keyboards","Sink handles",
            "Water fountain buttons","Microwave door handles",
            "Refrigerator door handles") # start with unordered list of labels

names(germs) = surfaces # assign the surface labels to each of the values 
                        # in the germs list using the names function

## Now sort the germs values, but this will now automatically 
## sort the surface labels to match the reorganisation of the values:
germs<-sort(germs, # sort the germs values (and now the assigned
                   # surface labels too)
            decreasing=TRUE) # sort values and label names by
                             # decreasing value

surfaces<-c(names(germs)) # name the reordered list of labels 'surfaces' again,
                          # using the assigned names from the germs list as they
                          # are organised now


#### 2.2.1.2	Refined pie chart: Adding colour, relative percentages, a legend (key),
#### and a chart title

pie(germs, # draw a pie chart of the 'germs' values 
    labels=surfaces, # use 'surfaces' to label segments
    clockwise=TRUE, # make segments run clockwise from 12 'o' clock position
    col= # add colours to segments
      rainbow( # use the rainbow function to choose colours
        length(surfaces))) # use as many different colours as there are segments

pie(germs, 
    labels=paste(germs,"%"), # label segments with the values and
                             # add a % symbol to each label
    clockwise=TRUE, 
    col= c("darkgoldenrod1", "orchid3", "springgreen3",
           "yellow2", "royalblue3")) # use these 5 colours for segments

piepercent<-round( # create list of rounded relative percentages
  100*germs/sum(germs), # calculate the relative percentages for each segment
  1) # round to one decimal place

pie(piepercent, 
    labels=paste(piepercent,"%"), # label segments with the relative percentages
                                  # and add a % symbol to each label
    clockwise=TRUE, 
    col= c("darkgoldenrod1", "orchid3", "springgreen3",
           "yellow2", "royalblue3"))

legend("bottomright", # set position of legend
       legend=surfaces, # list names to be included in legend
       cex=0.7, # choose font size
       fill=c("darkgoldenrod1", "orchid3", "springgreen3",
              "yellow2", "royalblue3")) # list colours to correspond with segments

pie(piepercent, 
    labels=paste(piepercent,"%"), 
    main="Which workplace surfaces harbour the most germs?", # include a title
    clockwise=TRUE, 
    col= c("darkgoldenrod1", "orchid3", "springgreen3",
           "yellow2", "royalblue3"))
legend("bottomright",
       legend=surfaces,
       cex=0.7,
       fill=c("darkgoldenrod1", "orchid3", "springgreen3",
              "yellow2", "royalblue3"))


### Ordinal data (ordered categories)

#### Refined pie chart

percent<-c(36,46,6,1,11) # create a list of the percentage values

responses<-c("Strongly support", "Tend to support", "Tend to oppose", 
             "Strongly oppose", "Don't know") # create a list of the responses

pie(percent, # draw pie chart with list of values
    labels=paste(percent,"%"), # label segments with the list of values
                               # and add a % symbol to each label
    clockwise=TRUE, # make segments run clockwise from 12 'o' clock position
    col= c("forestgreen", "green","red", "red4",
           "cornsilk2")) # use these 5 colours for segments

legend("bottomright", # set position of legend
       legend=responses, # list names to be included in legend
       cex=0.7, # choose font size
       fill=c("forestgreen", "green","red", "red4", 
              "cornsilk2")) # list colours to correspond with segments

pie(percent, 
    labels=paste(percent,"%"), 
    clockwise=TRUE, 
    col= c("forestgreen", "green","red", "red4", 
           "cornsilk2"),
    main="UK adult attitudes towards rewilding") # include a title
legend("bottomright",
       responses, 
       cex=0.7,
       fill=c("forestgreen", "green","red", "red4", 
              "cornsilk2"))
