R ggplot在图例项之间添加空间 [英] R ggplot add space between legend items

查看:34
本文介绍了R ggplot在图例项之间添加空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ggplot2,正在尝试增加图例项之间的间距.

下面的代码生成以下图:

 库(tidyverse)库(ggplot2)年<-2005:2015可变值<-1000:1010变量b<-1010:1020df = data.frame(year,variablea,variableb)df%>%聚集(变量,值,-年)%&%ggplot(aes(x =年,y =值,颜色=变量,线型=变量))+geom_line()+主题(legend.key = element_blank(),legend.text = element_text(size = 12),legend.position ="bottom",legend.title = element_blank(),panel.background = element_rect(fill ="transparent")#面板的背景色,plot.background = element_rect(fill ="transparent")#情节的背景) 

但是底部的图例项之间的间隙太小了,无法提供舒适感.

我试图使解决方案适应其他问题,但是它们不起作用.

例如,添加 + guides(fill = guide_legend(keywidth = 0.1,keyheight = 0.1,default.unit ="inch")),如

I've a ggplot2 and am trying to increase the spacing between legend items.

The code below produces the plot below:

library(tidyverse)
library(ggplot2)
year <- 2005:2015
variablea <- 1000:1010
variableb <- 1010:1020
df = data.frame(year, variablea, variableb)

df %>% gather(variable, value, -year) %>% 
  ggplot(aes(x = year, y = value, colour = variable, linetype = variable)) + 
  geom_line() +
  theme(
    legend.key=element_blank()
    ,legend.text=element_text(size=12)
    ,legend.position="bottom"
    ,legend.title=element_blank()
    ,panel.background = element_rect(fill = "transparent") # bg of the panel
    ,plot.background = element_rect(fill = "transparent") # bg of the plot
  ) 

However the gap between legend items at the bottom is too close for comfort.

I've tried to adapt solutions from other questions, but they don't work.

For example, adding + guides(fill=guide_legend(keywidth=0.1, keyheight=0.1, default.unit="inch")) as suggested in Is there a way to change the spacing between legend items in ggplot2? doesn't work because its a line graph, so the lines simply expand to fill the extra space. I've also tried the second solution suggested in the linked question, but it doesn't seem to change things (currently I'm using + scale_fill_manual(values=c("red","blue"), labels=c("variablea ","variableb "))).

Also, I'm not exactly sure how to adapt the solution given in Space between gpplot2 horizontal legend elements, because I'm not using factor variables as far as I can tell.

What can I do?

解决方案

You could try something like the following:

df1 <- df %>% gather(variable, value, -year)
df1$variable <- paste0(df1$variable,"\t\t\t\t\t") # enter more tabs if needed
df1 %>% 
  ggplot(aes(x = year, y = value, colour = variable, linetype = variable)) + 
  geom_line() +
  theme(
    legend.key=element_blank()
    ,legend.text=element_text(size=12)
    ,legend.position="bottom"
    ,legend.title=element_blank()
    ,panel.background = element_rect(fill = "transparent") # bg of the panel
    ,plot.background = element_rect(fill = "transparent") # bg of the plot
  )  + guides(linetype = guide_legend(ncol = 2,keywidth=4))

这篇关于R ggplot在图例项之间添加空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆