View source: R/plot_proportion.R
plot_proportion | R Documentation |
Bin a numerical column for the categorical x axis. Calculate the proportion of the rows that are present for each level of an additional factor column, like sex, by x-axis bins.
plot_proportion(
data,
column_factor,
column_bin,
digits = 0,
bar_width = c("n", "equal"),
...
)
data |
A data frame. |
column_factor |
< |
column_bin |
< |
digits |
A numeric value passed to the digits argument of |
bar_width |
A string of |
... |
Additional arguments that users want to pass to
|
ggmosaic is used to create a ggplot2 object for categorical data that can be modified upon return.
A ggplot2 object created with ggmosaic features.
Ian G. Taylor, Chantel R. Wetzel, Kelli F. Johnson
ggplot2::cut_width()
ggmosaic::geom_mosaic()
factor()
# Add presence/absence factor to data
temp <- catch_nwfsc_combo %>%
dplyr::mutate(new = factor(
cpue_kg_km2 <= 0,
levels = c(FALSE, TRUE),
labels = c("Present", "Absent")
))
# Plot depth bins (50 m) by presence/absence with default colors
plot_proportion(
data = temp,
column_factor = new,
column_bin = Depth_m,
width = 50,
boundary = 0
)
# Plot latitude bins (1 decimal degree) by presence/absence with custom
# colors
plot_proportion(
data = temp,
column_factor = new,
column_bin = Latitude_dd,
width = 1,
boundary = 0
) +
ggplot2::scale_fill_manual(values = c(
"darkorchid3",
grDevices::gray(0.7)
))
# Plot depth bins (25 m) by sex (F, M, U)
plot_proportion(
data = bio_nwfsc_combo %>%
dplyr::mutate(Sex = codify_sex(Sex)),
column_factor = Sex,
column_bin = Depth_m,
width = 25,
boundary = 0
)
# Change to equal sized bars
plot_proportion(
data = bio_nwfsc_combo %>%
dplyr::mutate(Sex = codify_sex(Sex)),
column_factor = Sex,
column_bin = Depth_m,
width = 25,
boundary = 0,
bar_width = "equal"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.