更改图例顺序以实现多种美学 [英] change order of legends for multiple aesthetics

查看:62
本文介绍了更改图例顺序以实现多种美学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个传奇-问题".

我有几种美学,希望指定每种美学的图例的绘制顺序.大多数线程都是为了改变内部的顺序,这不是我的问题.在我的示例中,我想指定填充图例的位置.有趣的是,在填充图例的顶部绘制了颜色图例,但是在底部绘制图例时,颜色图例位于填充图例的右侧".对于像我这样从左到右阅读的左右读者来说,这似乎有些随机.

该图显然有些随机,并且只是出于reprex的目的而快速制作的.

 库(ggplot2)ggplot(mtcars)+geom_boxplot(aes(cyl,hp,fill = as.character(gear)))+geom_boxplot(aes(cyl,disp,color = as.character(cyl)))+实验室(fill ='fill',color ='color')#在这里我希望填充图例在颜色图例之上 

  ggplot(mtcars)+geom_boxplot(aes(cyl,hp,fill = as.character(gear)))+geom_boxplot(aes(cyl,disp,color = as.character(cyl)))+实验室(fill ='fill',color ='color')+主题(legend.position ='bottom')#在这里我希望填充图例为* right *,颜色图例为左 

Another "legend - question".

I have several aesthetics and wish to specify the order in which the legends for each aesthetic are drawn. Most threads are about changing the order of the items within an aesthetic, but that's not my problem. In my example, I would like to specify the location of the fill legend. Funnily, the color legend is drawn on top of the fill legend, but "right" to the fill legend, when drawing the legend on the bottom. That seems somewhat random to left to right readers like me who also rather read from the top to the bottom.

The graph is obviously somewhat random and was just quickly made for reprex purpose.

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

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

Created on 2018-12-10 by the reprex package (v0.2.1)

解决方案

You can use the order option of 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))

这篇关于更改图例顺序以实现多种美学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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