Setup

library(tidyverse)
## ── Attaching packages ────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.0.0     ✔ purrr   0.2.5
## ✔ tibble  1.4.2     ✔ dplyr   0.7.6
## ✔ tidyr   0.8.1     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.0
## ── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
tree <- read_csv("data/tree.csv")
## Parsed with column specification:
## cols(
##   treeID = col_integer(),
##   stemID = col_integer(),
##   tag = col_integer(),
##   StemTag = col_integer(),
##   sp = col_character(),
##   quadrat = col_integer(),
##   gx = col_double(),
##   gy = col_double(),
##   MeasureID = col_integer(),
##   CensusID = col_integer(),
##   dbh = col_double(),
##   pom = col_double(),
##   hom = col_double(),
##   ExactDate = col_integer(),
##   DFstatus = col_character(),
##   codes = col_character(),
##   nostems = col_integer(),
##   status = col_character(),
##   date = col_character()
## )

Subset data

tree %>% 
  group_by(quadrat) %>% 
  summarise(n = n_distinct(sp)) %>% 
  arrange(desc(n)) %>% 
  top_n(1)
## Selecting by n
## # A tibble: 1 x 2
##   quadrat     n
##     <int> <int>
## 1    1516     8

Subset data

top <- filter(tree, quadrat == 1516)
top
## # A tibble: 11 x 19
##    treeID stemID    tag StemTag sp    quadrat    gx    gy MeasureID
##     <int>  <int>  <int>   <int> <chr>   <int> <dbl> <dbl>     <int>
##  1  23340  29166  14018   14018 DACE…    1516  298.  314.    442361
##  2  23345  29171  14022   14022 PREM…    1516  298.  309.    442364
##  3  69583  84987  82066   82066 CASA…    1516  282.  306.    463387
##  4  69752  85180  82249   82249 BYRS…    1516  285.  312.    463415
##  5  69918  85386  82442   82442 CECS…    1516  291.  315.    463447
##  6  70362  85902  82942   82942 TRIP…    1516  295.  315.    463505
##  7  70383  85926  82966   82966 SCHM…    1516  300.  315.    463506
##  8  70395  85938  82978   82978 SCHM…    1516  299.  314.    463507
##  9 101434 135554 144110  144110 CASA…    1516  297.  313.    478206
## 10 120221 157329 144119  144119 PREM…    1516  297.  310.    478212
## 11 126292     NA 177696      NA PSYB…    1516  293.  310.        NA
## # ... with 10 more variables: CensusID <int>, dbh <dbl>, pom <dbl>,
## #   hom <dbl>, ExactDate <int>, DFstatus <chr>, codes <chr>,
## #   nostems <int>, status <chr>, date <chr>

Visualize all species in one panel

ggplot(data = top, mapping = aes(x = gx, y = gy, colour = sp)) + 
  geom_point()

Visualize one species in each panel

ggplot(top, aes(gx, gy, colour = sp)) + 
  geom_point() +
  facet_wrap(vars(sp))

The layered grammar of graphics

https://ggplot2.tidyverse.org/reference/index.html

Learn more