ggplot 中图例项的间距

问题描述 投票:0回答:2

我想知道如何增加 ggplot 中图例项之间的间距。当您有更多涉及的绘图并且查看者需要清晰的颜色渐变图例时,这会派上用场,而当颜色渐变各部分之间没有足够的空白时,有时很难看到。

代表

data(iris)

iris$Sepal.Length.cut<-cut(iris$Sepal.Length, breaks=seq(4,8,1), labels=c("here","and here","here too", "here three"))

iris.grad<-colorRampPalette(c("darkgoldenrod","darkgoldenrod4"))
iris.col<-iris.grad(4)
names(iris.col)<-levels(iris$Sepal.Length.cut)

ggplot(iris)+
  geom_bar(aes(x=Species,fill=Sepal.Length.cut), stat="count")+
  scale_fill_manual(values=iris.col, name="increase space \n where indicated")+
  theme(legend.spacing.y=unit(1.5,"lines"))

legend.spacing.y 仅增加标题和图例元素之间的间距,增加键大小并不能解决问题。有趣的是,这个解决方案实际上说它应该并且他们的代表对我有用......也许它是条形和点之间的区别?

r ggplot2
2个回答
2
投票

您可以通过在向主题添加间距之前指定

guide_legend(byrow = TRUE)
来完成此操作。

ggplot(iris) +
  geom_bar(aes(Species, fill = Sepal.Length.cut), stat = "count") +
  scale_fill_manual(values=iris.col, name = "increase space \n where indicated") +
  guides(fill = guide_legend(byrow = TRUE)) +
  theme(legend.spacing.y = unit(1.5, "lines"))


0
投票

更新
从 ggplot2 3.5.0 开始,图例中的键/标签之间的间距由

legend.key.spacing.x
legend.key.spacing.y
控制。

这篇文章提供了更新的信息。

为其他登陆此处并正在寻找解决方案的人发帖。

© www.soinside.com 2019 - 2024. All rights reserved.