Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot dbh class by species (and maybe table it?) #52

Open
maurolepore opened this issue Mar 18, 2019 · 2 comments
Open

Plot dbh class by species (and maybe table it?) #52

maurolepore opened this issue Mar 18, 2019 · 2 comments

Comments

@maurolepore
Copy link
Contributor

David Kenfak proposed to add a funciton to easily plot dbh classes by species.

library(tidyverse)
#> Warning: package 'purrr' was built under R version 3.5.3
library(skimr)
#> Warning: package 'skimr' was built under R version 3.5.3
#> 
#> Attaching package: 'skimr'
#> The following object is masked from 'package:stats':
#> 
#>     filter

census <- fgeo.data::luquillo_tree5_random %>% 
  filter(!is.na(dbh)) %>% 
  filter(sp %in% unique(sp)[1:3])

# Plot
census %>% 
  group_by(sp) %>% 
  ggplot(aes(dbh)) +
  geom_histogram() +
  facet_wrap("sp", ncol = 1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2019-03-18 by the reprex package (v0.2.1)

--

A quick histogram may also be useful in the form of a table with the skimr package

library(tidyverse)
# Table
census %>%
  select(sp, matches("dbh")) %>% 
  group_by(sp) %>% 
  skim(dbh)

image

@maurolepore
Copy link
Contributor Author

maurolepore commented May 8, 2019

# Setup --------------------------------------------------------------------
suppressPackageStartupMessages(library(ggplot2))

plot_dbh_by_species <- function(data, 
                                binwidth = NULL, 
                                labels = NULL, 
                                theme = theme_classic()) {
  lapply(
    split(data, data$sp), 
    plot_dbh, 
    binwidth = binwidth, 
    labels = labels, 
    theme = theme
  )
}

plot_dbh <- function(data, binwidth, labels, theme) {
  ggplot(data, aes(dbh)) +
    geom_histogram(binwidth = binwidth) +
    labels + 
    facet_wrap("sp", ncol = 1) +
    theme
}

# Parameters to tweak ------------------------------------------------------
# Replace with your data
census <- fgeo.x::tree6_3species
# Change the width of each bar or leave it as is for automatic width
binwidth <- NULL
# Change axes labels
labels <- labs(x = "Diameter", y = "Count")
# Change theme https://ggplot2.tidyverse.org/reference/index.html#section-themes
theme <- theme_classic()
# Change size of each plot in each page
height <- 5
width <- 8

# Print each plot to a page in a .pdf file ----------------------------------
pdf("plots.pdf", paper = "letter", height = height, width = width)
plot_dbh_by_species(census, binwidth, labels, theme)
dev.off()

Created on 2019-05-08 by the reprex package (v0.2.1)

Result

Each page of the .pdf file should now have a plot simlar to this one:

image

@maurolepore
Copy link
Contributor Author

Suggestions by David:

  • Remove top box (keep name)
  • Try to separate bars a bit
  • Ensure the xlimits are fixed for all species at the max(data$dbh)
  • Also, look at the possibility of using percentages in the Y axis

@maurolepore maurolepore reopened this May 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant