The ggcharts package currently offers two functions with a highlight parameter: bar_chart() and lollipop_chart(). The usage is the same for both functions.
library(ggcharts)
library(dplyr)
data("biomedicalrevenue")
revenue2018 <- biomedicalrevenue %>%
filter(year == 2018)In its most simple form the highlight feature can be used to highlight a single bar or lollipop.
bar_chart(
revenue2018,
company,
revenue,
top_n = 10,
highlight = "Roche"
)The color for the highlighted and non-highlighted values are automatically determined from the currently active ggcharts theme, i.e. ggcharts_get_theme(). Thus, changing the theme will change these colors.
ggcharts_set_theme("theme_ng")
bar_chart(
revenue2018,
company,
revenue,
top_n = 10,
highlight = "Roche"
)To set the highlight and non-highlight colors manually you will need to pass a highlight_spec() to the highlight argument.
ggcharts_set_theme("theme_ggcharts")
spec <- highlight_spec(
what = "Roche",
highlight_color = "black",
other_color = "lightgray"
)
bar_chart(
revenue2018,
company,
revenue,
top_n = 10,
highlight = spec
)To highlight more than one value pass a vector to highlight.
bar_chart(
revenue2018,
company,
revenue,
top_n = 10,
highlight = c("Roche", "Novartis")
)To highlight multiple values in different colors you will need to use a highlight_spec() again.
spec <- highlight_spec(
what = c("Roche", "Novartis"),
highlight_color = c("steelblue", "darkorange")
)
lollipop_chart(
revenue2018,
company,
revenue,
top_n = 10,
highlight = spec
)The highlight feature is particularly useful when used in conjunction with the facet feature.
biomedicalrevenue %>%
filter(year %in% c(2012, 2014, 2016, 2018)) %>%
bar_chart(
company,
revenue,
facet = year,
top_n = 12,
highlight = "Bayer"
)