I am having trouble adding a pattern to a single bar. I want to show that the data for the last (most recent) year is currently incomplete, as data is still being gathered.
I've tried to modify the suggestions posted here but couldn't make it work. Hope somebody here can help out.
My data (modified names for easier understanding):
mydata <- data.frame('Year' = as.numeric(c(1:4)),
'State' = as.factor('State X'),
'Value' = as.numeric(c(0.6250000, 0.6562500, 0.6388889, 0.6666667)),
'Done' = as.factor(c(rep('Yes', 3), 'No')))
This is what I want the bars to look like:
The last bar should have diagonal white stripes on top of the fill color. The code I currently have is:
library(ggplot2)
library(ggpattern)
Lim = max(mydata$Value, na.rm = T)*1.15
ggplot (data = mydata,
aes(x = Year,
y = Value,
fill = State,
pattern = Done)) +
geom_bar_pattern(stat = 'identity', position = position_dodge2(preserve = 'single'),
pattern_fill = "white",
pattern_angle = 45,
pattern_density = 0.1,
pattern_spacing = 0.025,
pattern_key_scale_factor = 0.6) +
scale_pattern_manual(values = c('Yes' = 'none', 'No' = 'stripe')) +
expand_limits(y = c(0, Lim)) +
scale_fill_manual(values = c("#96D7F0")) +
scale_y_continuous(labels = scales::percent) +
scale_x_continuous(breaks = mydata$Year, labels = c('1' = '1\n1994-2001', '2' = '2\n2002-2008',
'3' = '3\n2008-2020', '4' = '4\n2013-')) +
theme_minimal() +
theme(legend.title = element_blank(),
legend.position = 'none',
plot.title = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 18),
axis.title.y = element_text(size = 18),
axis.text = element_text(size = 16))
The current error message I get:
Error in if (pat == "regular_polygon" && is.numeric(params$pattern_shape)) params$pattern_shape <- "convex6" :
missing value where TRUE/FALSE needed
How can I make this code work?
Thank you so much in advance for your help!