View source: R/dumbbell_chart.R
| dumbbell_chart | R Documentation |
A dumbbell chart (also called a connected dot plot) displays two values per category as dots connected by a horizontal line. It is ideal for showing the change or gap between two groups, time points, or conditions across multiple categories — a clean alternative to grouped bar charts favoured by outlets such as the New York Times and The Economist.
dumbbell_chart(
data,
x1,
x2,
y,
x1Label = NULL,
x2Label = NULL,
col1 = "steelblue",
col2 = "crimson",
lineCol = "#aaaaaa",
lineWidth = 2,
circleRadius = 6,
sort = "none",
xtitle = NULL,
xtitleFontSize = 13,
title = NULL,
titleFontSize = 16,
xFontSize = 11,
yFontSize = 11,
legend = TRUE,
legendFontSize = 11,
font = "Verdana, Geneva, Tahoma, sans-serif",
bgcol = "white",
axisCol = "black",
width = NULL,
height = NULL
)
data |
The data frame containing the variables to consider. |
x1 |
The name of the column for the first value (left dot). |
x2 |
The name of the column for the second value (right dot). |
y |
The name of the categorical column used as row labels. |
x1Label |
Legend label for the first value. Defaults to the value of
|
x2Label |
Legend label for the second value. Defaults to the value of
|
col1 |
Color of the first dot. Defaults to |
col2 |
Color of the second dot. Defaults to |
lineCol |
Color of the line connecting the two dots. Defaults to
|
lineWidth |
Width of the connecting line in pixels. Defaults to
|
circleRadius |
Radius of the dots in pixels. Defaults to |
sort |
Whether to sort the rows. One of |
xtitle |
Optional. Title of the x-axis. |
xtitleFontSize |
Font size of the x-axis title. Defaults to |
title |
Optional. Title of the chart. |
titleFontSize |
Font size of the chart title. Defaults to |
xFontSize |
Font size of the x-axis tick labels. Defaults to |
yFontSize |
Font size of the y-axis category labels. Defaults to
|
legend |
Whether to display a legend. Defaults to |
legendFontSize |
Font size of the legend text. Defaults to |
font |
The font family used for all text. Defaults to
|
bgcol |
Background color of the SVG. Defaults to |
axisCol |
Color of axis lines, ticks, and labels. Defaults to
|
width |
Optional. The width of the SVG output. |
height |
Optional. The height of the SVG output. |
An SVG dumbbell chart.
# Life expectancy change between 1952 and 2007 for selected countries
life_exp <- data.frame(
country = c("Brazil", "China", "Egypt", "India",
"Japan", "Mexico", "Nigeria", "Turkey"),
year_1952 = c(50.9, 44.0, 41.9, 37.4, 63.0, 50.8, 36.3, 43.6),
year_2007 = c(72.4, 72.9, 71.3, 64.7, 82.6, 76.2, 46.9, 71.8)
)
dumbbell_chart(
data = life_exp,
x1 = "year_1952",
x2 = "year_2007",
y = "country",
x1Label = "1952",
x2Label = "2007",
title = "Life expectancy: 1952 vs 2007",
xtitle = "Life expectancy (years)",
sort = "ascending"
)
# Comparing city vs highway fuel economy by car class
library(ggplot2)
library(dplyr)
mpg_summary <- summarise(group_by(mpg, class),
city = mean(cty), highway = mean(hwy))
dumbbell_chart(
data = mpg_summary,
x1 = "city",
x2 = "highway",
y = "class",
x1Label = "City",
x2Label = "Highway",
col1 = "steelblue",
col2 = "darkorange",
title = "City vs highway fuel economy by class",
xtitle = "Miles per gallon",
sort = "ascending"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.