<分区>
我有一个 ggplot2 并且正在尝试增加图例项之间的间距。
下面的代码生成下面的图:
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
)
然而,底部图例项目之间的差距太小了,让人不舒服。
我试过从其他问题中改编解决方案,但它们不起作用。
例如添加+ guides(fill=guide_legend(keywidth=0.1, keyheight=0.1,
default.unit="inch"))
如 Is there a way to change the spacing between legend items in ggplot2? 中所建议不起作用,因为它是折线图,因此线条只是扩展以填充额外的空间。我也尝试了链接问题中建议的第二种解决方案,但它似乎没有改变(目前我正在使用 + scale_fill_manual(values=c("red","blue"), labels= c("变量a","变量b"))
).
此外,我不确定如何调整 Space between gpplot2 horizontal legend elements 中给出的解决方案,因为据我所知,我没有使用因子变量。
我能做什么?