|
1 | | -#' Plot Condition |
| 1 | +#' Plot Condition |
2 | 2 | #' |
3 | 3 | #' @param data A data frame of species condition from `species_condition(..., output = "soe")` |
4 | 4 | #' @param var species of interest to plot |
5 | 5 | #' @return A ggplot |
6 | | -#' |
| 6 | +#' |
7 | 7 | #' @export |
8 | 8 |
|
9 | | -plot_condition <- function(data, |
10 | | - var, |
11 | | - return = TRUE){ |
| 9 | +plot_condition <- function(data, var) { |
12 | 10 | condition <- data |> |
13 | | - dplyr::select(YEAR, |
14 | | - Species, |
15 | | - EPU, |
16 | | - DATA_VALUE) |> |
| 11 | + dplyr::select(YEAR, Species, EPU, DATA_VALUE) |> |
17 | 12 | dplyr::group_by(Species) |> |
18 | | - dplyr::mutate(scaleCond = scale(DATA_VALUE,scale =T,center=T)) |
19 | | - |
20 | | - xs <- quantile(condition$scaleCond, seq(0,1, length.out = 6), na.rm = TRUE) |
21 | | - |
| 13 | + dplyr::mutate(scaleCond = scale(DATA_VALUE, scale = T, center = T)) |
| 14 | + |
| 15 | + xs <- quantile(condition$scaleCond, seq(0, 1, length.out = 6), na.rm = TRUE) |
| 16 | + |
22 | 17 | condition <- condition |> |
23 | | - dplyr::mutate(category = cut(scaleCond, |
24 | | - breaks = xs, |
25 | | - labels = c( "Poor Condition", |
26 | | - "Below Average", |
27 | | - "Neutral", |
28 | | - "Above Average", |
29 | | - "Good Condition"), |
30 | | - include.lowest = TRUE)) |
31 | | - |
| 18 | + dplyr::mutate( |
| 19 | + category = cut( |
| 20 | + scaleCond, |
| 21 | + breaks = xs, |
| 22 | + labels = c( |
| 23 | + "Poor Condition", |
| 24 | + "Below Average", |
| 25 | + "Neutral", |
| 26 | + "Above Average", |
| 27 | + "Good Condition" |
| 28 | + ), |
| 29 | + include.lowest = TRUE |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
32 | 33 | condition <- condition |> |
33 | 34 | dplyr::filter(Species %in% var) |> |
34 | 35 | dplyr::ungroup() |> |
35 | 36 | dplyr::arrange(YEAR) |> |
36 | 37 | dplyr::group_by(EPU) |> |
37 | | - dplyr::mutate(mean = mean(DATA_VALUE, na.rm = TRUE), |
38 | | - sd = sd(DATA_VALUE, na.rm = TRUE)) |> |
39 | | - ggplot2::ggplot(ggplot2::aes(x = YEAR, |
40 | | - y = DATA_VALUE, |
41 | | - color = category, |
42 | | - shape = EPU |
| 38 | + dplyr::mutate( |
| 39 | + mean = mean(DATA_VALUE, na.rm = TRUE), |
| 40 | + sd = sd(DATA_VALUE, na.rm = TRUE) |
| 41 | + ) |> |
| 42 | + ggplot2::ggplot(ggplot2::aes( |
| 43 | + x = YEAR, |
| 44 | + y = DATA_VALUE, |
| 45 | + color = category, |
| 46 | + shape = EPU |
43 | 47 | )) + |
44 | 48 | ggplot2::geom_path(color = "black", lty = 2, alpha = 0.5) + |
45 | 49 | ggplot2::geom_point(cex = 3) + |
46 | 50 | ggplot2::xlim(c(1989, 2024)) + |
47 | 51 | ggplot2::theme_classic(base_size = 16) + |
48 | | - ggplot2::theme(strip.text = ggplot2::element_text(size = 16), |
49 | | - axis.title.x = ggplot2::element_blank(), |
50 | | - aspect.ratio = 0.4, |
51 | | - legend.direction = "vertical", |
52 | | - legend.box = "horizontal") + |
| 52 | + ggplot2::theme( |
| 53 | + strip.text = ggplot2::element_text(size = 16), |
| 54 | + axis.title.x = ggplot2::element_blank(), |
| 55 | + aspect.ratio = 0.4, |
| 56 | + legend.direction = "vertical", |
| 57 | + legend.box = "horizontal" |
| 58 | + ) + |
53 | 59 | viridis::scale_color_viridis(discrete = TRUE) |
54 | | - |
| 60 | + |
55 | 61 | return(condition) |
56 | 62 | } |
57 | | - |
58 | | - |
|
0 commit comments