Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot2 – how to add a break between dotted lines in legend?

The following code produces two histograms. I’m wondering whether there’s any simple way to create a break in the dotted lines in the legend for these histograms. See the legend in the attached image – I want the dotted lines not to run into each other?


#make low cost histogram

PLOT1 <- LCset1 %>% 
  ggplot(aes(x=donation, fill=lowmonetaryamount))+
  coord_cartesian(xlim = c(1, NA)) +
  geom_histogram(binwidth=1,aes(y = (..count..)/sum(..count..)),col=I("black"))+
  scale_color_grey()+scale_fill_grey(start = .85,
                                     end = .85,) +
  theme_linedraw()+
  guides(fill = "none", cols='none')+
  geom_vline(aes(xintercept=10, size='\nLow cost\n'),
             color="black", linetype=5)+
  geom_vline(aes(xintercept=50, size='\nHigh cost\n'),
             color="black", linetype="dotted")+
  scale_size_manual(values = c(.5, 0.5), breaks = c('\nLow cost\n','\nHigh cost\n'), guide=guide_legend(title = "", override.aes = list(linetype=c(5,3), color=c('black', 'black'))))+
  scale_y_continuous(labels=scales::percent, limits = c(0, .015))+
  scale_x_continuous(breaks = c(seq(0,50,10), 55), labels = c("","$10","$20","$30","$40","$50","      >$50"), limits = c(0, 60))+
  ggtitle("\n% donating each non-zero amount when low cost is $10")+
  theme(plot.title = element_text(hjust = 0.5))+
  xlab(NULL)+
  ylab(NULL)+
  theme(plot.title = element_text(size=13))+
  theme(
    # other theme parameters
    legend.key.size = unit(2, "lines"),     
    legend.key.width = unit(1, "lines"),
    legend.text = element_text(size=12),
    axis.text = element_text(size=12),
    legend.key.height = unit(0.5, "lines")
  )

#make high cost histogram 

PLOT2 <- HCset1 %>% 
  ggplot(aes(x=donation, fill=lowmonetaryamount))+
  coord_cartesian(xlim = c(1, NA)) +
  geom_histogram(binwidth=1,aes(y = (..count..)/sum(..count..)),col=I("black"))+
  scale_color_grey()+scale_fill_grey(start = .85,
                                     end = .85,) +
  theme_linedraw()+
  guides(fill = "none", cols='none')+
  geom_vline(aes(xintercept=10, size='\nLow cost\n'),
             color="black", linetype=5)+
  geom_vline(aes(xintercept=50, size='\nHigh cost\n'),
             color="black", linetype="dotted")+
  scale_size_manual(values = c(.5, 0.5), breaks = c('\nLow cost\n','\nHigh cost\n'), guide=guide_legend(title = "", override.aes = list(linetype=c(5,3), color=c('black', 'black'))))+
  scale_y_continuous(labels=scales::percent, limits = c(0, .015))+
  scale_x_continuous(breaks = c(seq(0,50,10), 55), labels = c("","$10","$20","$30","$40","$50","      >$50"), limits = c(0, 60))+
  ggtitle("% donating each non-zero amount when high cost is $50")+
  theme(plot.title = element_text(hjust = 0.5))+
  xlab(NULL)+
  ylab(NULL)+
  theme(plot.title = element_text(size=13))+
  theme(
    # other theme parameters
    legend.key.size = unit(2, "lines"),     
    legend.key.width = unit(1, "lines"),
    legend.text = element_text(size=12),
    axis.text = element_text(size=12),
    legend.key.height = unit(0.5, "lines")
  )

#combine two histograms

plot<- ggarrange(PLOT1, PLOT2,ncol=1,label.x=1,common.legend = TRUE, legend="right")
plots <- grid.arrange(plot,top=textGrob("Histograms for the 40.1% of the total sample whose last donation was between $10.00 and $49.99",gp=gpar(fontsize=14,fontface="bold")))

ggsave("plots_1.png", plots, width = 10, height = 8)

I tried adding legend.spacing.y to the theme section, but this only moves the entire legend down. I’d like to create a distinct break so that the two dotted lines don’t touch each other.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The trick to making legend.spacing.y work is the byrow=TRUE param on guide_legend

library(ggplot2)
ggplot(mtcars, aes(y = factor(cyl), fill = factor(cyl))) +
  geom_bar() +
  theme(legend.spacing.y = unit(2.0, 'cm')) +
  guides(fill = guide_legend(byrow = TRUE))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading