Introduction

Getting started

#load packages
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)

Getting started with data

We will use a made up dataset on polar bears

data <- read.csv("bears.csv")
head(data, 5)
##     bearID  Seal Beluga Walrus Bodycond Sex Ageclass Year Era  Region
## 1 BEAR0001 54.58  43.32   2.10    85.38   F Subadult 2002 Mid Central
## 2 BEAR0002 60.38  33.73   5.89    65.55   M    Adult 2002 Mid Central
## 3 BEAR0003 54.92  39.07   6.01    75.30   M Subadult 2002 Mid Central
## 4 BEAR0005 82.06   9.01   8.94    80.30   F Subadult 2002 Mid    West
## 5 BEAR0006 56.66  43.33   0.00    81.97   M Subadult 2002 Mid    West
##     Lat    Long
## 1 71.03 -118.30
## 2 71.52 -119.48
## 3 70.27 -117.32
## 4 70.25 -131.10
## 5 69.75 -132.30

Exploratory

#Density plot
ggplot(data, aes(x=Seal))+geom_density()

ggplot(data, aes(x=Seal))+geom_density(color="red")

ggplot(data, aes(x=Seal))+geom_density(fill="blue", color="red")

#Density plot: group by
ggplot(data, aes(x=Bodycond,color=Region, fill=Region)) + geom_density()

ggplot(data, aes(x=Bodycond,color=Region, fill=Region)) + geom_density(alpha=0.4)

ggplot(data, aes(x=Bodycond,color=Region, fill=Region)) + geom_density(alpha=0.4) + scale_color_manual(values=c("red", "green", "blue"))

#Histogram
ggplot(data, aes(x=Seal))+geom_histogram(binwidth=10)

ggplot(data, aes(x=Seal))+geom_histogram(color="black",fill="blue", binwidth=10)

ggplot(data, aes(x=Seal))+geom_histogram(color="black",fill="blue", alpha=0.5, binwidth=10)

#Histogram: group by
ggplot(data, aes(x=Bodycond,color=Region, fill=Region)) + geom_histogram(alpha=0.7, binwidth=5)

ggplot(data, aes(x=Bodycond,color=Region, fill=Region)) + geom_histogram(alpha=0.7, binwidth=5,position=position_dodge())#seperate

#Dotplot
ggplot(data, aes(x=Seal))+geom_dotplot(alpha=0.4, binwidth=2.5, fill="red")

#Dotplot: group by
ggplot(data, aes(x=Bodycond,color=Region, fill=Region)) + geom_dotplot(alpha=0.7, binwidth=2.5)

Two variables

X Continuous, Y Continuous

#Scatter plot
ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()

ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_point()

ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_point()+scale_color_manual(values=c("black", "red"))

#Add smooth line
ggplot(data, aes(x=Seal, y=Bodycond))+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#Add a lm line
ggplot(data, aes(x=Seal, y=Bodycond))+geom_smooth(method=lm)

ggplot(data, aes(x=Seal, y=Bodycond))+geom_smooth(method=lm, level=0.5) #level=alpha

ggplot(data, aes(x=Seal, y=Bodycond))+geom_smooth(method=lm, se=FALSE)

#layer!
ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_point()+scale_color_manual(values=c("black", "red"))+geom_smooth(method=lm, se=FALSE)

#Line plot
ggplot(data, aes(x=Seal, y=Bodycond))+geom_line()

ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_line()

X Continuous, Y Continuous

#Boxplot
ggplot(data, aes(x=Region, y=Seal))+geom_boxplot(alpha=0.5)

#Boxplot: group by
ggplot(data, aes(x=Region, y=Seal))+geom_boxplot(alpha=0.5, aes(fill = Sex, color=Sex))

#Barplot
ggplot(data=data, aes(x=Region, y=Seal)) + geom_bar(stat="identity")

ggplot(data=data, aes(x=Region, y=Seal)) + geom_bar(aes(fill=Sex), stat="identity")

ggplot(data=data, aes(x=Region, y=Seal)) + geom_bar(aes(fill=Sex), stat="identity", position=position_dodge())

#Violin plot
ggplot(data, aes(x=Region, y=Seal)) + geom_violin(fill="black", alpha=0.4)

Themes

ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+theme_grey()

ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+theme_classic()

ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+theme_bw()

ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+theme_minimal()

#Theme modifications
ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+theme_grey()+ theme(axis.text.x = element_text(size=20))

ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+theme_grey()+ theme(panel.grid.major = element_line(color= "black"))

ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+theme_grey()+theme_grey()+  theme(plot.background = element_rect(fill="green"))

Labels and titles

#add titles
ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+labs(x = "x axis title", y = "y axis title", title="a nice title")

#split up text
ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+labs(x = "x axis title", y = "y axis title", title="a nice\ntitle")

ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()+labs(x = "x axis title", y = "y axis title", title="a nice\ntitle")+theme(plot.title = element_text(hjust = 0.5))

Legends

ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_point()+ scale_color_manual(values=c("black","blue"), name = "New title", breaks = c("M","F"), labels = c("Males","Females"))

ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_point()+ scale_color_manual(values=c("black","blue"), name = "New title", breaks = c("M","F"), labels = c("Males","Females")) +theme(legend.position="top")

Scale/colors

#basic point colors
ggplot(data, aes(x=Seal, y=Bodycond))+geom_point(color="blue")

#qualitative
ggplot(data=data, aes(x=Region, y=Seal)) + geom_bar(aes(fill=Ageclass), stat="identity", position=position_dodge())

ggplot(data=data, aes(x=Region, y=Seal)) + geom_bar(aes(fill=Ageclass), stat="identity", position=position_dodge())+ scale_fill_brewer(palette = "Accent")

#sequential
ggplot(data, aes(x=Seal, y=Bodycond))+geom_point()

ggplot(data, aes(x=Seal, y=Bodycond))+geom_point(aes(color=Year))+scale_fill_brewer(palette = "gradient")
## Warning in pal_name(palette, type): Unknown palette gradient

Point shapes and sizes

#shape by factor variable
ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_point(aes(shape=Sex)) +scale_color_manual(values=c("black", "blue"))

#manual shape selection
ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_point(aes(shape=Sex)) +scale_color_manual(values=c("black", "blue"))+scale_shape_manual(values=c(3,4))

#size by continuous variable
ggplot(data, aes(x=Seal, y=Bodycond, color=Sex))+geom_point(aes(size=Walrus)) +scale_color_manual(values=c("blue", "red"))

#see shapes
shapes <- data.frame(
  shape = c(0:19, 22, 21, 24, 23, 20),
  x = 0:24 %/% 5,
  y = -(0:24 %% 5)
)
ggplot(shapes, aes(x, y)) + 
  geom_point(aes(shape = shape), size = 5, fill = "red") +
  geom_text(aes(label = shape), hjust = 0, nudge_x = 0.15) +
  scale_shape_identity() +
  expand_limits(x = 4.1) +
  scale_x_continuous(NULL, breaks = NULL) + 
  scale_y_continuous(NULL, breaks = NULL)

Facetting

#gather so we can facet
datag <- data %>% gather(Prey, Proportion, Seal:Walrus)

#facet vertical
ggplot(data=datag, aes(x=Prey, y=Proportion, fill=Sex)) + geom_bar(stat="identity", position=position_dodge(0.9))+facet_grid(~Ageclass)

#facet horizontal
ggplot(data=datag, aes(x=Prey, y=Proportion, fill=Sex)) + geom_bar(stat="identity", position=position_dodge(0.9))+facet_grid(Region~.)

#facet vertical and horizontal
ggplot(data=datag, aes(x=Prey, y=Proportion, fill=Sex)) + geom_bar(stat="identity", position=position_dodge(0.9))+facet_grid(Region~Ageclass)

Centroids and error bars

centroids <- aggregate(cbind(Bodycond,Seal)~Sex,data,mean)
f         <- function(z)sd(z)/sqrt(length(z)) # function to calculate std.err
se        <- aggregate(cbind(se.x=Bodycond,se.y=Seal)~Sex,data,sd)
centroids <- merge(centroids,se, by="Sex")    # add std.err column to centroids
ggplot(data, aes(Bodycond,Seal,color=Sex))+
  geom_point(size=1)+
  geom_point(data=centroids, aes(Bodycond,Seal,color=Sex),size=4)+
  geom_errorbar(data=centroids,aes(ymin=Seal-se.y,ymax=Seal+se.y),width=1)+
  geom_errorbarh(data=centroids,aes(xmin=Bodycond-se.x,xmax=Bodycond+se.x),height=2)+scale_color_manual(values=c("black", "blue"))

More advanced: mean line graphs

#take mean value
myData <- aggregate(datag$Proportion,by = list(Prey = datag$Prey, Year = datag$Year, Sex = datag$Sex, Region = datag$Region),FUN = function(x) c(mean = mean(x), sd = sd(x),n = length(x)))
myData1 <- do.call(data.frame, myData)
myData1$se <- myData1$x.sd / sqrt(myData1$x.n)
colnames(myData1) <- c("Prey", "Year","Sex", "Region","mean", "sd", "n", "se")
myData1$names <- c(paste(myData1$Prey, "Prey /", myData1$Year, "Year"))
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = myData1$mean + myData1$se,ymin = myData1$mean - myData1$se)

##For lipid
myDatab <- aggregate(datag$Bodycond,by = list(Year = datag$Year, Sex = datag$Sex, Region = datag$Region, Prey = datag$Prey),FUN = function(x) c(mean = mean(x), sd = sd(x),n = length(x)))
myDatabc <- do.call(data.frame, myDatab)
myDatabc$se <- myDatabc$x.sd / sqrt(myDatabc$x.n)
colnames(myDatabc) <- c("Year","Sex", "Region","Prey","mean", "sd", "n", "se")
myDatabc$names <- c(paste(myDatabc$Preyspecies, "Prey /", myDatabc$Year, "Year"))
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = myDatabc$mean + myDatabc$se,ymin = myDatabc$mean - myDatabc$se)

####This is a plot with colors of all prey on one graph
p<- ggplot(myData1, aes(x=Year, y=mean, group=Prey, color=Prey, shape = Prey)) +
  geom_line()+
  geom_point(size=2) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean + se),width = .25, na.rm = TRUE) +
  labs(x="Year", y = "Estimated contribution to polar bear diet (%)")+theme(panel.background = element_rect(fill = 'grey98', colour = 'black'), plot.title = element_text(hjust = 'center')) + theme_bw() + scale_x_continuous(breaks = pretty(myDatabc$Year, n = 5)) +facet_grid(Sex~Region, scales="free_x")+geom_line(data=myDatabc,aes(color="Bodycond", group="Prey"), size=1)+geom_point(data=myDatabc, aes(color="Bodycond", shape="Bodycond"))+geom_errorbar(data = myDatabc, aes(ymin=mean-se, ymax=mean + se),width = .25, na.rm = TRUE, color="black")+scale_color_manual(values=c("lightskyblue","black","violetred3","turquoise4"))+scale_shape_manual(values=c(15:18,4))+scale_y_continuous("Estimated contribution to polar bear diet (%)", sec.axis = sec_axis(~ .* 1.00, name = "Body condition"))+theme(plot.title = element_text(hjust = 0.5))+theme_grey()
p