又一个“传奇-问题”。
我有几种美学,并希望指定每种美学的图例的绘制顺序。
大多数主题都是关于更改项目的顺序within审美,但这不是我的问题。在我的示例中,我想指定填充图例的位置。有趣的是,颜色图例绘制在填充图例的顶部,但是当在底部绘制图例时,颜色图例绘制在填充图例的“右侧”。对于像我这样也喜欢从上到下阅读的从左到右的读者来说,这似乎有点随机。
该图显然有些随机,只是为了表示目的而快速制作的。
library(ggplot2)
ggplot(mtcars) +
geom_boxplot(aes(cyl, hp, fill = as.character(gear))) +
geom_boxplot(aes(cyl, disp, color = as.character(cyl))) +
labs(fill = 'fill', color = 'color')
# here I would like the fill legend to be *above* the color legend
![](https://i.stack.imgur.com/BkjN0.png)
ggplot(mtcars) +
geom_boxplot(aes(cyl, hp, fill = as.character(gear))) +
geom_boxplot(aes(cyl, disp, color = as.character(cyl))) +
labs(fill = 'fill', color = 'color') +
theme(legend.position = 'bottom')
# here I would like the fill legend to be *right* and the color legend left
![](https://i.stack.imgur.com/GHLBf.png)
Created on 2018-12-10 by the reprex package (v0.2.1)
您可以使用order
的选项guide_legend
:
ggplot(mtcars) +
geom_boxplot(aes(cyl, hp, fill = as.character(gear))) +
geom_boxplot(aes(cyl, disp, color = as.character(cyl))) +
labs(fill = 'fill', color = 'color') +
guides(fill = guide_legend(order = 1),
color = guide_legend(order = 2))
![enter image description here](https://i.stack.imgur.com/YQ0bA.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)