I need to use gghighlight in a clustered bar chart in R in order to highlight only one single bar. My code and sample data looks like this:
library(tidyr)
library(ggplot2)
dat <- data.frame(country=c('USA','Brazil','Ghana','England','Australia'), Stabbing=c(15,10,9,6,7), Accidents=c(20,25,21,28,15), Suicide=c(3,10,7,8,6))
dat.m <- melt(dat, id.vars='country')
dat.g <- gather(dat, type, value, -country)
ggplot(dat.g, aes(type, value)) +
geom_bar(aes(fill = country), stat = "identity", position = "dodge") +
gghighlight(type == "Accidents" & country == "Brazil")
But this gives me this awkward
How can I get gghighlight to highlight only one single bar of one group (so combining two conditions for two discrete variables)?
I think gghighlight is not built for this kind of plot - not yet! You could file a feature request ? It is a bit unclear though if this visualisation is very helpful. Gghighlight always draws everything - this makes the "weird" shadows when dodging.
If you want to keep using gghightlight, maybe try faceting, which they suggest in their vignette
A suggestion - Use facets:
(using
mtcars
as example)Created on 2020-03-09 by the reprex package (v0.3.0)
Or, here without
gghighlight
, in a more traditional subsetting approach. You need to make a subset of data which contains rows for each group you want to dodge by, in this case "cyl" and "gear". I replace the irrelevant data with "NA", you could also use "0".Created on 2020-03-10 by the reprex package (v0.3.0)