首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ggplot2:当参考线看起来没有效果时,强制图例占一行

ggplot2:当参考线看起来没有效果时,强制图例占一行
EN

Stack Overflow用户
提问于 2018-01-05 09:38:36
回答 1查看 1.9K关注 0票数 3

我有以下玩具示例:

library(ggplot2)
g <- ggplot(mpg, aes(class))
mpg$drv <- sample(as.character((-4:4)), size = length(mpg$drv), replace = T)
g + geom_bar(aes(fill = drv), position = "fill") + theme(legend.position = "bottom") + guides(colour = guide_legend(nrow = 1))

对我来说

生成下图。

尽管我可能会尝试,但我无法将底部的图例放在一行中。我环顾四周,guide_legend似乎没有任何效果。有没有更好的方法来做到这一点,或者有没有另一种方法来强制图例排成一行?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-05 09:46:12

您在guides中指定了colour而不是fill。试着这样做:

library(ggplot2)
mpg$drv <- sample(as.character((-4:4)), size = length(mpg$drv), replace = T)
g <- ggplot(mpg, aes(class))
g + geom_bar(aes(fill = drv), position = "fill") + theme(legend.position = "bottom") + guides(fill = guide_legend(nrow = 1))
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48106036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档