ggplot2 – 如何在图例中在虚线之间添加间隔?

huangapple go评论68阅读模式
英文:

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

问题

以下是要翻译的内容:

这段代码生成了两个直方图。我想知道是否有简单的方法来在图例中为这两个直方图的虚线创建一个间隔,就像附带的图像中的图例一样 - 我希望虚线不要相互交错?

我尝试在主题部分添加了 legend.spacing.y,但这只是将整个图例向下移动。我想创建一个明显的间隔,使这两条虚线不会相互接触。

英文:

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.

答案1

得分: 2

使legend.spacing.y起作用的关键是在guide_legend上使用byrow=TRUE参数。

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))
英文:

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))

huangapple
  • 本文由 发表于 2023年7月12日 23:09:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定