Install

Install ggplot2

#install.packages("ggplot2") I place the hashtag here after I run the code so that I don't keep installing in on my runs.

#installing the package from a zip file
#location of file: "C:\Users\jessi\Desktop\Rdata\TA_biostats\plyr_1.8.9"

#install.packages("plyr")

library(ggplot2)
library(plyr)
library(PNWColors)
#install.packages("wesanderson")
library(wesanderson)

Read

Read in your data, and check it out!

islandData <- read.csv("island.csv")

head(islandData) #prints out the first few lines of data
##    Island  Face AlgalDensity Nutrients
## 1 SanJuan North        47.59 11.296981
## 2 SanJuan North        39.56  8.796913
## 3 SanJuan North        47.62 10.399961
## 4 SanJuan North        53.82  9.903147
## 5 SanJuan North        50.64  6.947449
## 6 SanJuan South        46.43 14.702849
dim(islandData) #tells you row number and column number
## [1] 45  4

Using DDPLY

A way to split up your data, and add new data to it.

ddply(islandData, c("Island", "Face"), summarize, meanDensity= mean(AlgalDensity), meanNuts = mean(Nutrients), sdDensity = sd(AlgalDensity), sdNuts=sd(Nutrients), n=length(AlgalDensity), seDensity=(sd(AlgalDensity)/sqrt(n)))
##    Island  Face meanDensity meanNuts sdDensity   sdNuts n seDensity
## 1   Lopez North      46.792 18.61833  3.764143 6.766023 5  1.683376
## 2   Lopez South      43.350 19.97105  9.359137 2.791610 5  4.185533
## 3   Lopez  West      48.950 18.65613  7.052794 8.420359 5  3.154105
## 4 SanJuan North      47.846  9.46889  5.297866 1.673813 5  2.369278
## 5 SanJuan South      38.774 11.73487  6.533960 4.192207 5  2.922076
## 6 SanJuan  West      39.522 11.40644  7.617189 2.919711 5  3.406511
## 7    Shaw North      47.674 22.93506  4.619143 3.964185 5  2.065743
## 8    Shaw South      44.230 24.14472 10.716602 3.924830 5  4.792610
## 9    Shaw  West      38.234 23.56055  6.361233 4.456273 5  2.844830

Wow, this just did so much to our data! It seems that we kept “Island” and “Face” columns using the concatenate function. Next, we added summarize. I still need to figure out what that is for. Then we started listing off parameters, and took things like the mean of the AgalDensity column. We told the function that our sample size was equal to the length of our IslandData, and we found error bars using the error bar equation. (*which btw is in your textbook)

 sum_island <- ddply(islandData, c("Island","Face"), summarize,
meanDensity=mean(AlgalDensity), meanNuts=mean(Nutrients),
sdDensity=sd(AlgalDensity), sdNuts=sd(Nutrients), n=length(AlgalDensity),
seDensity=(sd(AlgalDensity)/sqrt(n)))

Now we added all of those calculations to a new data frame called “sum_island”.

Error Bars

moth <- pnw_palette("Moth", 9)
stars <- pnw_palette("Starfish", 1)

pal <- wes_palette("Zissou1", 9, type = "continuous")

ggplot(sum_island, aes(x=meanDensity, y=meanNuts))+
  geom_point(color=pal)+
  geom_errorbar(aes(ymin=meanNuts-sdNuts, ymax=meanNuts+sdNuts), color= pal, width=0.3)+
  labs(title = "Do nutrients make algae happy?", x="Mean Algal Density", y = "Mean Nutrients") #width tells the horizontal width of the bar. 

#try 1, now try 0.5, see the difference?

Some algae seem fine with low nutrients. I wonder which direction they are facing?

QPLOT out of commission?

If you tried qplot() you probably noticed that it does not work anymore. Use ggplot() instead.

FUN STUFF

northface <- subset(islandData, Face=="North")
southface <- subset(islandData, Face=="South")
eastface <- subset(islandData, Face =="East")
westface <- subset(islandData, Face=="West")

starfish3 <- pnw_palette("Starfish", 3)

ggplot(northface, aes(AlgalDensity))+
  geom_histogram(bins=7, fill=stars)+
  labs(title="Algal Density on the north face", x="Algal Density")

ggplot(southface, aes(AlgalDensity))+
  geom_histogram(bins=7, fill="#73e1e2")+
  labs(title="Algal Density on the north face", x="Algal Density")

ggplot(westface, aes(AlgalDensity))+
  geom_histogram(bins=7, fill="#82a9ae")+
  labs(title="Algal Density on the north face", x="Algal Density")

islandcolors <- c("#015b58", "#89689d", "#e69b99")

ggplot(islandData, aes(x=Island, y=AlgalDensity))+
  geom_boxplot(color=islandcolors)+
  labs(title = "Which location has the highest algal density?", x="", y = "Algal Density")

It seems that Lopez island, on average, has the highest density.