Generate a line plot with all of Gradient's best practices applied. Almost instantly ready for publication.
Usage
g6tr_line_plot(
df,
x,
y,
group = NULL,
conf_int = NULL,
conf_int_alpha = 0.4,
include_point = TRUE,
display_text = c("all", "highlighted", "none"),
scale_y = "percent",
y_lim = NULL,
x_wrap = 30,
accuracy = 1,
size = 3,
linewidth = size * 0.5,
text_size = size * 2,
highlight = NULL,
highlight_condition = NULL,
highlight_color = g6tr_pal("core")(1),
highlight_color_other = "grey",
color_scale = NULL,
n_insert = 2,
n_border = 1,
...
)Arguments
- df
Data frame that holds x, y, group and/or conf_int columns.
- x
Unquoted X variable. A character/factor variable on the x-axis.
- y
Unquoted Y variable. A continuous variable on the y-axis.
- group
Unquoted variable. A value associated with each point/label on the plot.
- conf_int
Unquoted confidence interval variable. If present will add error bars to the plot.
- conf_int_alpha
Numeric. Alpha value for
conf_int.- include_point
Logical. Should points be included in plot?.
- display_text
Character. How text should be displayed. One of
all(default) (apply text labels to every bar),highlighted(apply text label to only items passed inhighlight) ornone(don't apply text labels).- scale_y
Character. How should the labels and x axis be formatted. One of
percent,dollar,noneor a function e.gscales::comma.- y_lim
Numeric. Limits on the y axis.
- x_wrap
Numeric x wrap length. Default
30.- accuracy
Numeric. Passed to the
accuracyargument of the selectedscalefunction to set numeric accuracy. Default is1.- size
Numeric. Size of point.
- linewidth
Numeric. Size of line.
- text_size
Numeric. Size of text.
- highlight
Character. Item(s) from
groupto be highlighted in the plot.- highlight_condition
Character. (Default is
NULL) A filter expression to highlight items by.- highlight_color, highlight_color_other
Character. Valid color (6 digit hex-code or must consist in
grDevices::colors()) for the highlights of the plot. By default, whenhighlightisn't specified, all points are g6tr core blue and whenhighlightis specified, highlighted points are displayed in g6tr core blue while the other points are in grey. If arguments are passed tohighlight,highlight_colorhandles the color of the highlighted fields andhighlight_color_otherhandles the colors of the fields not highlighted.- color_scale
Character. Valid color (6 digit hex-code or must consist in
grDevices::colors()) to be used whengroupis present. Passed toscale_fill_manual()is used.- n_insert
Numeric. Number of points to insert between each pair of original points
- n_border
Numeric. Number of points to add at the beginning and at the end
- ...
Additional arguments passed to
ggrepel::geom_text_repel().
Examples
library(stringr)
library(dplyr)
library(ggplot2)
library(tidyr)
## Prepare fake data
x_duration <- c(
"June 2020",
"October 2020",
"June 2021",
"November 2021",
"April 2022",
"September 2022"
)
set.seed(2132)
sample_lp_data <-
expand_grid(
duration = factor(x_duration, levels = x_duration),
levels = c(
"Support: Increasing housing density",
"Oppose: Increasing housing density",
"No Opinion: Increasing housing density",
"Support: Ending parking requirements",
"Oppose: Ending parking requirements",
"No Opinion: Ending parking requirements"
)
) %>%
mutate(
perc_agree =
case_when(
str_detect(levels, "^Support") ~
sample(45:60, nrow(.), replace = TRUE) / 100,
str_detect(levels, "^Oppose") ~
sample(20:30, nrow(.), replace = TRUE) / 100,
TRUE ~ sample(5:10, nrow(.), replace = TRUE) / 100
)
) %>%
mutate(conf_int = sample(1:7, nrow(.), replace = TRUE) / 100)
## Create a basic line plot
sample_lp_data %>%
g6tr_line_plot(
x = duration,
y = perc_agree,
group = levels,
conf_int = conf_int,
include_point = TRUE,
n_insert = 10,
n_border = 1
)
#> Warning: No color scale provided. Defaulting to g6tr colors
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_point()`).
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_text_repel()`).
## Add some highlighting
sample_lp_data %>%
g6tr_line_plot(
x = duration,
y = perc_agree,
group = levels,
conf_int = conf_int,
include_point = TRUE,
display_text = "highlighted",
highlight = "Support: Increasing housing density",
n_insert = 10,
n_border = 1
)
#> Warning: No color scale provided. Defaulting to g6tr colors
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_point()`).
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_text_repel()`).
## Highlight based on a filter expression
sample_lp_data %>%
g6tr_line_plot(
x = duration,
y = perc_agree,
group = levels,
conf_int = conf_int,
include_point = TRUE,
display_text = "highlighted",
highlight_condition = "perc_agree > 0.35",
n_insert = 10,
n_border = 1
)
#> Warning: No color scale provided. Defaulting to g6tr colors
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_point()`).
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_text_repel()`).