trevorld / ggpattern

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

guide_legend not working with ggpattern #36

Closed M-Colley closed 3 years ago

M-Colley commented 3 years ago

Hey there, i am trying to get the legend to be plotted in one line. My code is shown in the following:

ggplot(long2chi, aes(x = Year, y = value, fill=variable)) +
  ggpattern::geom_bar_pattern(aes(pattern_spacing =variable, pattern_linetype = variable, pattern_yoffset = variable, pattern_fill = variable, pattern_angle = variable, pattern_density = variable),
                              fill= 'grey90',
                              stat = "identity",
                              pattern = "stripe",
                              position = position_dodge(preserve = 'single')) +
  scale_pattern_density_manual(values = c(Vision = 0.4, Hearing = 0.4, Psychological = 0.4, General = 0.4, "Brain & Cognitive" =0.4, Mobility = 0.4, "Elderly" = 0.4))+
  scale_pattern_angle_manual(values = c(Vision = 0, Hearing = 40, Psychological = 70, General = 120, "Brain & Cognitive" =150, Mobility = 220, "Elderly" = 300))+
  ylab("Publications") +
  scale_y_continuous(breaks = seq(0, 40, 5.0)) + 
  xlab("Year")+
  scale_x_continuous(breaks = seq(2016, 2019, 1.0)) + 
  ggtitle("Title") + 
  theme_bw()+
  theme(legend.title = element_blank(), legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0, unit = "mm"))+
  theme(legend.position = "bottom")+
  theme(legend.key.width = unit(1.0, "cm"), legend.key.height = unit(1, "cm"),legend.justification = "left", legend.direction = "horizontal")+
  theme(axis.text=element_text(size=13),axis.title=element_text(size=15,face="bold"), legend.text = element_text(size = 5)) + 
  **guides(fill=guide_legend(nrow= 1)**)

Created on 2021-05-25 by the reprex package (v2.0.0)

When I use the same code but use geom_bar(stat = "identity", position = position_dodge(preserve = 'single'))+ instead of using ggpattern, everything works fine. Do you have an idea why this could be?

trevorld commented 3 years ago

Please supply a minimal, reproducible example. Thanks!

trevorld commented 3 years ago

No minimal, reproducible example provided.