Get familiarized with metadata - Acacia drepanolobium Surveys

ggplot

Data

acacia <- read.csv("http://www.esapubs.org/archive/ecol/E095/064/ACACIA_DREPANOLOBIUM_SURVEY.txt", sep="\t", na.strings = c("dead"))

Basics

library(ggplot2)
ggplot(data = acacia, mapping = aes(x = CIRC, y = HEIGHT))
ggplot(data = acacia, mapping = aes(x = CIRC, y = HEIGHT)) +
  geom_point()
ggplot(data = acacia, mapping = aes(x = CIRC, y = HEIGHT)) +
  geom_point(size = 3, color = "blue", alpha = 0.5)
ggplot(data = acacia, mapping = aes(x = CIRC, y = HEIGHT)) +
  geom_point(size = 3, color = "blue", alpha = 0.5) +
  labs(x = "Circumference [cm]", y = "Height [m]",
       title = "Acacia Survey at UHURU")

Do Task 1 in Acacia and ants.

Grouping

ggplot(acacia, aes(x = CIRC, y = HEIGHT, color = TREATMENT)) +
  geom_point(size = 3, alpha = 0.5)
ggplot(acacia, aes(x = CIRC, y = HEIGHT)) +
  geom_point(size = 3, alpha = 0.5) +
  facet_wrap(~TREATMENT)

Do Tasks 2-3 in Acacia and ants.

Rescaling axes

ggplot(data = acacia, mapping = aes(x = CIRC, y = HEIGHT)) +
  geom_point(size = 3, color = "blue", alpha = 0.5) +
  scale_y_log10() +
  scale_x_log10()

Assign Mass vs Metabolism and Tasks 1-4 in Adult vs Newborn Size.

Layers

ggplot(acacia, aes(x = CIRC, y = HEIGHT)) +
  geom_point()
ggplot(acacia, aes(x = CIRC, y = HEIGHT)) +
  geom_point() +
  geom_smooth(method = "lm")
ggplot(acacia, aes(x = CIRC, y = HEIGHT, color = TREATMENT)) +
  geom_point() +
  geom_smooth(method = "lm")

Do Task 4 in Acacia and ants.

Statistical transformations

ggplot(acacia, aes(x = TREATMENT)) +
  geom_bar()
ggplot(acacia, aes(x = CIRC)) +
  geom_histogram(fill = "red")
ggplot(acacia, aes(x = CIRC)) +
  geom_histogram(fill = "red", bins = 15)
ggplot(acacia, aes(x = CIRC)) +
  geom_histogram(fill = "red", binwidth = 5)

Do Tasks 1-2 in Acacia and ants histograms.

Changing values across layers

ggplot(data = acacia, mapping = aes(x = CIRC, y = HEIGHT)) +
  geom_point()
ggplot() +
  geom_point(data = acacia,
             mapping = aes(x = CIRC, y = HEIGHT,
                           color = TREATMENT))
ggplot() +
  geom_point(data = acacia,
             mapping = aes(x = CIRC, y = HEIGHT)) +
                           color = TREATMENT))
  geom_smooth()
ggplot() +
  geom_point(data = acacia,
             mapping = aes(x = CIRC, y = HEIGHT,
                           color = TREATMENT)) +
  geom_smooth(data = acacia,
              mapping = aes(x = CIRC, y = HEIGHT))

Do Task 3 in Acacia and ants histograms.

Grammar of graphics

Saving plots as new files

ggsave(“acacia_by_treatment.jpg”)
ggsave(“figures/acacia_by_treatment.pdf”, height = 5, width = 5)

Assign the rest of the exercises.

Combining different datasets (time allowing)

library(readr)
trees <- read_tsv("data/TREE_SURVEYS.txt")
ggplot() +
  geom_point(data = trees,
             aes(x = CIRC, y = HEIGHT),
             color = "gray") +
  geom_point(data = acacia,
             aes(x = CIRC, y = HEIGHT),
             color = "red") +
  labs(x = "Circumference [cm]", y = "Height [m]")
ggplot(mapping = aes(x = CIRC, y = HEIGHT)) +
  geom_point(data = trees, color = "gray") +
  geom_point(data = acacia, color = "red") +
  labs(x = "Circumference [cm]", y = "Height [m]")