Generate a simple diverging plot with all of Gradient's best practices applied. Almost instantly ready for publication.
Usage
g6tr_diverging_plot(
df,
x,
y,
center = 0,
display_text = c("all", "highlighted", "none"),
highlight = NULL,
highlight_condition = FALSE,
bar_color = g6tr_pal("core")(1),
bar_color_other = "grey",
text_color = "black",
text_color_other = "grey",
bar_order = c("none", "asc", "desc"),
text_fontface = c("bold", "plain", "italic", "bold.italic"),
text_position = c("outside", "inside"),
scale = "percent",
accuracy = 1,
text_size = 8,
bar_width = 0.7,
segment_width = 1.5,
wrap_length = 25,
point_size = NULL,
x_label_pos = c("left", "side"),
side_text_color = "black",
geom_type = c("segment", "bar"),
expand = 0.1,
label_threshold = NULL,
conf_int_low = NULL,
conf_int_high = NULL,
error_color = "grey",
hide_segment_line = FALSE
)Arguments
- df
Data frame that holds x and y columns.
- x
Unquoted X variable. A character or factor variable on the x-axis.
- y
Unquoted Y variable.
- center
Numeric. Indicates where plot originates from. Default is set to 0.
- 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).- highlight
Character
orFilter expression. Item(s) fromxto be highlighted in the plot.highlightcan also be a filter expression. Use a filter expression when you need to highlight elements fromxbased on other columns in the data.- highlight_condition
Logical. Should
highlightbe considered as a filter expression.- bar_color, bar_color_other
Character. Valid color (6 digit hex-code or must consist in
grDevices::colors()) for the bars and highlights of the plot. By default, whenhighlightisn't specified, all bars are g6tr core blue (#36c8ef) and whenhighlightis specified, highlighted bars are displayed in g6tr core blue while the other bars are in grey. If arguments are passed tohighlight,bar_colorhandles the color of the highlighted bars andbar_color_otherhandles the colors of the bars not highlighted.- text_color, text_color_other
Character. Valid color (6 digit hex-code or must consist in
grDevices::colors()) for the text color to be displayed on highlighted/non highlighted fields. If arguments are passed tohighlight,text_colorhandles the color of the highlighted text andtext_color_otherhandles the colors of the unhighlighted texts.- bar_order
Character. How bars should be sorted. One of
"none"(default),"asc"or"desc".- text_fontface
Character. One of
bold(default),"plain","italic"or"bold.italic".- text_position
Character. One of
outside(default) orinside.- scale
How should the labels and x axis be formatted. One of
"percent","dollar","none"or a function e.gscales::comma.- accuracy
Numeric. Passed to the
accuracyargument of the selectedscalefunction to set numeric accuracy. Default is1.- text_size
Numeric. Size of the text passed to
ggplot2::geom_text().- bar_width
Numeric (defaults to
0.7). Width of the bars ranging from0-1.- segment_width
Numeric. Size to be passed to
ggplot2::geom_segment().- wrap_length
Numeric vector. passed to
scales::wrap_format()as width.- point_size
Integer. Size of point when using
geom_type = "segment".- x_label_pos
Character. Where should the x label be? One of
"left"(default) or"side".- side_text_color
Character. When
x_label_posis set to side, what should the text color be? Defaults to black.- geom_type
Character. What type of plot should be produced? One of
segment(lollipop chart) orbar(bar plot).- expand
Numeric. Value to be passed the
multargument ofggplot2::expansion()default is 0.1.- label_threshold
Values below this threshold will not be displayed.
- conf_int_low
Unquoted upper limit variable.
- conf_int_high
Unquoted lower limit variable.
- error_color
Error bar color.
- hide_segment_line
Should the segment line be hidden? Useful when displaying error bars in a lollipop chart.
Examples
library(tibble)
library(dplyr)
library(ggplot2)
sample_data <-
tribble(
~x, ~y,
###########
"Nike", .87,
"Riley", .34,
"Nissan", .98,
"AT7", .76,
"Cheggs", .56,
"J&J", .67,
"AAA", -.20,
"Peach", -.40,
"Can", -.50
) |> mutate(low = y - 0.2, high = y + 0.2)
## g6tr_diverging_plot() work in two ways (bar or segment)
## Let's explore its most basic form
g6tr_diverging_plot(
df = sample_data,
x = x,
y = y,
bar_order = "desc",
display_text = "all",
x_label_pos = "left",
text_position = "inside",
text_fontface = "bold",
geom_type = "bar",
text_color = "white"
) +
labs(x = NULL, y = NULL)
g6tr_diverging_plot(
df = sample_data,
x = x,
y = y,
bar_order = "desc",
display_text = "all",
x_label_pos = "left",
text_position = "inside",
text_fontface = "bold",
geom_type = "segment",
text_color = "white"
) +
labs(x = NULL, y = NULL)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> ℹ The deprecated feature was likely used in the g6tr.theme package.
#> Please report the issue to the authors.
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
## Let's take it a step further with its highlighting
## feature and changing the x label position.
g6tr_diverging_plot(
df = sample_data,
x = x,
y = y,
bar_order = "desc",
display_text = "all",
point_size = 21,
x_label_pos = "side",
text_position = "outside",
highlight = c("AT7", "AAA"),
bar_color = c("#50bd90", "#EF8150"),
bar_color_other = "#36c8ef",
text_color = c("#50bd90", "#EF8150"),
text_color_other = "#36c8ef",
text_fontface = "bold",
geom_type = "bar"
) +
labs(x = NULL, y = NULL)
g6tr_diverging_plot(
df = sample_data,
x = x,
y = y,
bar_order = "desc",
display_text = "all",
point_size = 21,
x_label_pos = "side",
text_position = "outside",
highlight = "between(y, -0.2, 0.7)",
highlight_condition = TRUE,
bar_color = "#EF8150",
bar_color_other = "#36c8ef",
text_color = "#50bd90",
text_color_other = "#36c8ef",
text_fontface = "bold",
geom_type = "bar"
) +
labs(x = NULL, y = NULL)
g6tr_diverging_plot(
df = sample_data,
x = x,
y = y,
bar_order = "desc",
display_text = "all",
x_label_pos = "side",
text_position = "outside",
highlight = c("AT7", "AAA"),
bar_color = c("#50bd90", "#EF8150"),
bar_color_other = "#36c8ef",
text_color = c("#50bd90", "#EF8150"),
text_color_other = "#36c8ef",
text_fontface = "bold",
geom_type = "segment"
) +
labs(x = NULL, y = NULL)
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
## To finesse this, let's recreate the famous MD plot
g6tr_diverging_plot(
df = sample_data,
x = x,
y = y,
center = 0,
bar_order = "desc",
display_text = "all",
point_size = 30,
x_label_pos = "left",
text_position = "inside",
highlight = c("AAA", "Peach", "Can"),
bar_color = c("#de425b"),
bar_color_other = "#50bd90",
text_color = c("#FFFFFF"),
text_color_other = c("#FFFFFF"),
text_fontface = "bold",
geom_type = "segment"
) +
labs(x = NULL, y = NULL) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = "grey90")
)
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
## Include error bars
sample_data %>%
g6tr_diverging_plot(
x = x,
y = y,
display_text = "all",
bar_order = "desc",
conf_int_high = high,
conf_int_low = low,
hide_segment_line = TRUE,
text_position = "inside"
)
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
sample_data %>%
g6tr_diverging_plot(
x = x,
y = y,
display_text = "all",
bar_order = "desc",
conf_int_high = high,
conf_int_low = low,
geom_type = "bar"
)