The ggcharts package currently offers two functions with a highlight parameter: bar_chart() and lollipop_chart(). Unless otherwise noted 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,
limit = 10,
highlight = "Roche"
)Changing the highlight color is simple: just provide a color to the bar_color parameter.
bar_chart(
revenue2018,
company,
revenue,
limit = 10,
highlight = "Roche",
bar_color = "darkorange"
)To change the highlight color in lollipop_chart() pass a color to line_color. Note that having a different color for the lollipop head and stick is not possible when passing a value to highlight. The point_color will simply be ignored.
lollipop_chart(
revenue2018,
company,
revenue,
limit = 10,
highlight = "Roche",
line_color = "darkgreen"
)To highlight more than one bar pass a vector to highlight. Note the message that gets printed.
bar_chart(
revenue2018,
company,
revenue,
limit = 10,
highlight = c("Roche", "Novartis")
)#> Using the same color to highlight all bars.
To highlight multiple bars in different colors pass a vector of colors to bar_color or line_color for bar_chart() and lollipop_chart(), respectively.
lollipop_chart(
revenue2018,
company,
revenue,
limit = 10,
highlight = c("Roche", "Novartis"),
line_color = c("steelblue", "darkorange")
)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,
limit = 12,
highlight = "Bayer",
bar_color = "darkgreen"
)