Assignment 1 for ANT 191

Before the assignment

Loading packages

My preferred first step is to load all the packages I know I will be using for the project. I set up a chunk and as I use a function that is not native to R, I add the inclusive package to a list within the chunk. Note that this does not install the package, rather it only loads them. Packages must have been installed previously.

library(knitr)
library(readr)
library(dplyr)
library(igraph)

Question 1

Loading data

Ensure your data are in the form of a CSV file (that is with the filename extension “.csv”).

read.csv("~/Desktop/ANT191WD/measles.csv")

Once your data are imported, make them an object using this symbol: <-

meas <- read.csv("~/Desktop/ANT191WD/measles.csv")

Here I have chosen to call them “meas”

Plotting the data

The object below represents a barplot of a matrix created from the first two columns in “meas”, that is “year” and “measles.cases”. After the creation of the matrix, I change the aesthetic qualities of the plot, such as the color, labels, and main title.

measbar <- barplot(height=t(as.matrix(meas[,1:2])), names.arg = meas$year, col="orangered3", xlab="Year", ylab="Reported Cases", main="Incidence of measles cases by year 1980-2017")

some text

Export the plot as a PDF to save it as a vector image instead of a raster.

Bonus question

Combining plots

Superimposing two plots can be accomplished using the function and argument par(new=T). The first part of the code is identical to the line above, creating our bar plot. This is followed by a new line with the function and argument listed above. Next, a new plot is created with the factor “vaccination.coverage” from our “meas” dataset. The axes and title have been removed from the second plot and the next line with the function axis() replaces the y axis and the argument side=4 moves it to the right side of the plot. The final line of the chunk labels the second y axis. The argument line=-1 moves the label left of the axis.

measbar <- barplot(height=t(as.matrix(meas[,1:2])), names.arg = meas$year, col="orangered3", xlab="Year", ylab="Reported Cases", main="Incidence of measles cases by year 1980-2017")
par(new=T)
plot(meas$year, meas$vaccination.coverage,col="midnightblue", axes=F, ann=F, type="l", lwd=3)
axis(side=4)
mtext(text="% vaccinated", side=4, line=-1)

some text

Question 2

Trimming the data

Rather than trimming the data in a spreadsheet, using the filter() function in the package dplyr will accomplish the same thing within R. Essentially you create another dataset that is a subset of the dataset we are currently working with. As filter() exists as a function in multiple packages, we use this expression: dplyr::filter() to specify we want to use the function included in the package dplyr.

meastrim <- meas %>% dplyr::filter(meas$year <= c("2005"), meas$year >= c("1985"))

The scatter plot is generated using the plot() function.

meastrimplot <- plot(meastrim$vaccination.coverage, meastrim$measles.cases, col="orangered3", pch=16, xlab="Year", ylab="Reported Cases", main="Effects of vaccination coverage \non measles cases (1985-2005)")

some text

Question 3

Matrices

g <- sample_gnp(20, 1/10)
plot(g, vertex.color="deepskyblue4", vertex.label.color="white", vertex.size=30, edge.curved=.2, main="gnp matrix")

some text

The matrix displayed above was created from the sample_gnp() function. The name derives from two of the main arguments; number of nodes, n= (set to 20) and the probability of two nodes connecting, p= (1/10). The two other arguments implied in the code are that the edges are not directed, and no loop connects a node to itself.