| plot_risk | R Documentation |
estimate_risk()Generate horizontal bar charts of the risk estimates generated
from estimate_risk()
plot_risk(
risk_dat,
add_to_dat = TRUE,
collapse = is.data.frame(risk_dat),
progress = TRUE,
outcomes = "all",
color_scheme = "single",
color_dat = get_default_color("single"),
color_for_last_group = get_default_color("threshold_final"),
annotation = "all",
legend = TRUE,
lines = TRUE,
line_text = TRUE,
base_size = 12
)
risk_dat |
Data frame or list of data frames (required). This either
needs to be from the output of |
add_to_dat |
Logical (optional behavior variable): Whether to add the
plots as a list column to the input passed via |
collapse |
Logical (optional behavior variable): Whether to collapse the
output into a single data frame if applicable, either |
progress |
Logical (optional behavior variable): Whether to display a
progress bar during execution, either |
outcomes |
Character (optional behavior variable): The outcome(s) to plot.
This should be a character vector listing the outcomes in the order of
desired plotting from top to bottom. The default of |
color_scheme |
Character (optional behavior variable): The color scheme
to use, one of "single" (the default) or "categories". This argument is
interdependent with argument |
color_dat |
Character or data frame (optional behavior variable): This
argument is interdependent with argument
|
color_for_last_group |
Character (optional behavior variable): The color
to use for the de facto last group (i.e., values that fall at or above
the highest valid user-specified threshold in the |
annotation |
Character (optional behavior variable): Whether to include all annotation ("all"), no annotation ("none"), or one or more of the components of annotation, which can be one or more of "title", "subtitle", and "caption". The default is "all". Note this does not impact the legend (where applicable). |
legend |
Logical (optional behavior variable): Whether to include a
legend with the plot, either |
lines |
Logical (optional behavior variable): Whether to include
dashed vertical lines at the threshold values, either |
line_text |
Logical (optional behavior variable): Whether to include
caption text describing the values at which the lines are drawn (i.e., the
threshold values), either |
base_size |
Numeric (optional behavior variable): The base font size to use for the plot. The default is 12. Entries that are not a single, finite, positive number will be discarded in favor of the default. |
color_dat and color_for_last_group when color_scheme = "categories"See also the color_dat and color_scheme arguments. An example color_dat
data frame might be something like the following:
color_dat_v1 <- data.frame(
threshold = 0.075,
color = "#00A65A"
)
# or
color_dat_v2 <- data.frame(
threshold = c(0.075, 0.20),
color = c("#00A65A", "#FFFDAF")
)
# or
color_dat_v3 <- data.frame(
threshold = c(0.075, 0.20, 0.35),
color = c("#00A65A", "#FFFDAF", "#FF8C00")
)
# Not-great entries that will be cleaned
color_dat_why_v1 <- data.frame(
threshold = c(0.075, 0.20, 0.20),
color = c("#00A65A", "#FFFDAF", "#FF8C00")
)
color_dat_why_v2 <- data.frame(
threshold = c(0.35, 0.075, 1.5),
color = c("#00A65A", "#FFFDAF", "#FF8C00")
)
In all the above cases, users can specify color_for_last_group, and
plot_risk() will automatically assign that color to values at or above the
highest valid user-specified threshold. For example, suppose color_for_last_group
is set as "#dd4b39" (this is also the default color for this argument). Using
the above examples:
For color_dat_v1, "#00A65A" will be applied for values below 0.075, and
"#dd4b39" will be applied for values at or above 0.075.
For color_dat_v2, "#00A65A" will be applied for values below 0.075, "#FFFDAF"
will be applied for values at or above 0.075 and below 0.20, and "#dd4b39"
will be applied for values at or above 0.20.
For color_dat_v3, "#00A65A" will be applied for values below 0.075, "#FFFDAF"
will be applied for values at or above 0.075 and below 0.20, "#FF8C00" will
be applied for values at or above 0.20 and below 0.35, and "#dd4b39" will be
applied for values at or above 0.35.
For color_dat_why_v1, the function will clean the data frame to remove the
duplicate threshold value, and the result will be the same as color_dat_v2.
For color_dat_why_v2, the function will clean the data frame to remove the
invalid threshold value of 1.5, then the thresholds will be arranged in
increasing order, so the final result will be threshold 1 being at 0.075 with
color "#FFFDAF", threshold 2 being at 0.35 with color "#00A65A", and the final
group being at or above 0.35 with color "#dd4b39".
An object in one of the following formats:
a single ggplot object
a list of ggplot objects
a data frame with ggplots in the column plot
a list of data frames with ggplots in the column plot
Which of these formats is returned depends on the values of the arguments
add_to_dat and collapse and the structure of risk_dat. The table below
summarizes the return format for various input/argument combinations:
Structure of risk_dat | Value of add_to_dat | Value of collapse | Output format |
| data frame | TRUE | not applicable | data frame with plot list-column |
| data frame | FALSE | not applicable | ggplot object or list of ggplot objects |
| list of data frames | TRUE | TRUE | single, collapsed data frame with plot list-column |
| list of data frames | TRUE | FALSE | list of data frames, each with plot list-column |
| list of data frames | FALSE | not applicable | list of ggplot objects |
See the vignette "plot-risk" for more information and examples demonstrating these various return formats.
res <- estimate_risk(
age = 50,
sex = "female",
sbp = 160,
bp_tx = TRUE,
total_c = 200,
hdl_c = 45,
statin = FALSE,
dm = TRUE,
smoking = FALSE,
egfr = 90,
bmi = 35
)
plot_risk(res, add_to_dat = FALSE)
# Remove annotation
plot_risk(res, annotation = "none", add_to_dat = FALSE)
# Plot only a subset of the outcomes
# (e.g., excluding total CVD and heart failure)
plot_risk(
res,
outcomes = c("ascvd", "chd", "stroke"),
add_to_dat = FALSE
)
# Need to plot risk estimates you already have? No problem.
risk_dat <- data.frame(
total_cvd = 0.15,
ascvd = 0.10,
heart_failure = 0.05,
chd = 0.07,
stroke = 0.03,
model = "base",
over_years = 10
)
plot_risk(risk_dat, add_to_dat = FALSE)
# Rest of examples limited to interactive sessions
res_10yr <- res$risk_est_10yr
res_30yr <- res$risk_est_30yr
# Change color for `color_scheme = "single"`
plot_risk(
res_10yr,
color_scheme = "single",
color_dat = "darkgreen",
add_to_dat = FALSE
)
# Use `color_scheme = "categories"`
color_dat <- data.frame(
threshold = c(0.075, 0.20),
color = c("#00A65A", "#FF8C00")
)
plot_risk(
res_30yr,
color_scheme = "categories",
color_dat = color_dat,
add_to_dat = FALSE
)
# Change color for final group
plot_risk(
res_30yr,
color_scheme = "categories",
color_dat = color_dat,
color_for_last_group = "maroon",
add_to_dat = FALSE
)
# Remove legend
plot_risk(
res_10yr,
color_scheme = "categories",
color_dat = color_dat,
legend = FALSE,
add_to_dat = FALSE
)
# Remove legend and lines
plot_risk(
res_10yr,
color_scheme = "categories",
color_dat = color_dat,
legend = FALSE,
lines = FALSE,
add_to_dat = FALSE
)
# Remove legend and line text (but keep lines)
plot_risk(
res_10yr,
color_scheme = "categories",
color_dat = color_dat,
legend = FALSE,
line_text = FALSE,
add_to_dat = FALSE
)
# Run `plot_risk()` on a data frame of results from
# `estimate_risk()`/`est_risk()`
dat <- data.frame(
age = c(40, 50, 60),
sex = c("female", "female", "male"),
sbp = c(160, 120, 140),
bp_tx = c(TRUE, TRUE, FALSE),
total_c = c(200, 189, 156),
hdl_c = c(45, 42, 38),
statin = c(FALSE, TRUE, TRUE),
dm = c(TRUE, FALSE, TRUE),
smoking = c(FALSE, TRUE, FALSE),
egfr = c(90, 85, 88),
bmi = c(35, 22, 28)
)
res <- estimate_risk(use_dat = dat)
plot_risk(res, add_to_dat = FALSE) # Returns plots
plot_risk(res) # Returns data frame `plot` list-column
# Because the plots are ggplot objects, they can be further customized
# like any other ggplot object.
res_10yr <- estimate_risk(use_dat = dat[1, ], time = 10)
# Customization via {ggplot2}
p <- plot_risk(res_10yr, add_to_dat = FALSE)
# Note `labs()`, `theme()`, and `margin()` are from {ggplot2}, so one would
# need to get access to them via, e.g., `library(ggplot2)`, `ggplot2::` prefixing,
# `importFrom()` (if developing a package; for example, {preventr} `importFrom()`s
# all three), etc.
p +
ggplot2::labs(title = "Lorem ipsum") +
ggplot2::theme(plot.margin = ggplot2::margin(20, 20, 20, 20))
# etc.
# It is also easy to combine the 10- and 30-year plots if desired, e.g.,
# via packages like {patchwork} with functions like `patchwork::wrap_plots()`,
# and there are many additional options for composing plots via {patchwork}.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.