trevorld / ggpattern

ggplot geoms with pattern fills
https://trevorldavis.com/R/ggpattern/dev/
Other
360 stars 18 forks source link

Cannot have a legend without a pattern in it. #8

Closed nfparsons closed 2 years ago

nfparsons commented 4 years ago

When I plot using fills and patterns I get image 1. Screen Shot 2020-06-03 at 11 43 46 AM

What I'm looking for is image 2.

Screen Shot 2020-06-03 at 3 58 30 PM

Here's my code:

dat.fac.p %>% filter(visn == 1) %>% ggplot(aes(x = station, y = value, group = visn)) + geom_col_pattern( aes(pattern_density = plot_pattern), fill = 'white', pattern = 'crosshatch', pattern_spacing = 0.02 ) + geom_col( aes(fill = plot_fill), color = 'gray50', alpha = 0.5 ) + scale_pattern_density_manual(name = element_blank(), values = c(a = 0.1, b = 0), labels = c("Positive", "")) + scale_fill_viridis(name = element_blank(), discrete = TRUE, labels = c("Screened", "Not Screened")) + theme_ipsum()

complexgenome commented 4 years ago

can you share sample data?

M-Colley commented 4 years ago

Maybe related:

ggpattern::scale_pattern_fill_manual(values=c(scale_fill_colors4[2],scale_fill_colors4[3]), breaks=c("example1", "example2"), values= c("test1", "test2"))

gives me two legends instead of one where the legends is correctly named (i.e., in this case "test1" etc)

jkeast commented 4 years ago

I'm having the same issue. I copied the pie graph example from the Geometry-based Patterns vignette to illustrate and added a new variable to map the fill aesthetic to. This is what I get: ggpattern_issue_example

Are there any work-arounds? Modifying pattern_key_scale_factor isn't helping.

jkeast commented 4 years ago

I found a work-around with override.aes! Here's the code, last line is the important one:

df <- data.frame(
  group = factor(c("Cool", "But", "Use", "Less"), levels = c("Cool", "But", "Use", "Less")),
  value = c(10, 20, 30, 40),
  other = factor(c("a", "b", "c", "d"),levels = c("a", "b", "c", "d"))
)

ggplot(df, aes(x="", y = value, pattern = group, pattern_angle = group))+
  geom_bar_pattern(aes(fill=other), width = 1, stat = "identity", colour = 'black', pattern_aspect_ratio = 1, pattern_density = 0.3, pattern_key_scale_factor = .5) +
  coord_polar("y", start=0) +
  theme_void(20) +
  theme(legend.key.size = unit(1, 'cm')) +
  labs(title = "ggpattern::geom_bar_pattern() + coord_polar()")+
  guides(fill = guide_legend(override.aes = list(pattern = c("none", "none", "none", "none"))))

and the fixed plot: ggpattern_issue_example2

trevorld commented 2 years ago

@nfparsons, @jkeast seems to provide a solution for legends with "two separate groups" separately controlling color and patterns case. An alternative solution would be to create 3 groups: "Positive" (and screened), "Screened" (and not positive), "Not Screened" and then use the crosshatch pattern for the "Positive" group and the "none" pattern for the other two groups and then color the "Not Screened" group yellow and the other two groups purple.