1

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:
example graph

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!

benson23
  • 16,369
  • 9
  • 19
  • 38
Keni
  • 63
  • 5
  • It runs for me without any issues. Try restarting your R session to reset the attached libraries. Another thing to look for--if this is part of a larger script file, what other libraries did you call, and are they conflicting? You can check this by ensuring that `ggpattern` and `ggplot2` (or `tidyverse`) are the last two called (so they have precedence). Since you changed the data a bit for this question, did you try it with this data? – Kat Feb 15 '22 at 14:03
  • You're right, after restarting R this code works almost perfectly now. The only problem that remains is that the color is black instead of white. How do I fix that? – Keni Feb 15 '22 at 15:26
  • If you're referring to the stripes or lines on the one with the pattern, you need to set the *color*, not *fill*. Use the parameter `pattern_color = "white"` within `geom_bar_pattern()`. – Kat Feb 16 '22 at 00:09
  • @Kat Late update but: it works now, thank you so much for your help! – Keni Feb 22 '22 at 07:43

0 Answers0