使用guide_legend()反转图例 [英] reversed legend using guide_legend()

查看:118
本文介绍了使用guide_legend()反转图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绝对是 ggplot2 的初学者,并且正在练习使用 guide_legend()函数.我不太确定为什么 guide_legend()会颠倒图例的顺序.

这是基本示例:

  df<-data.frame(x = 1,y = 1:3,z = 1:3)基本<-ggplot(df,aes(x,y))+ geom_raster(aes(fill = z)) 

这很好地生成了一个光栅图.

但是,当我添加以下行时,图例的顺序被颠倒了.有人可以解释为什么会这样吗?

  base + scale_fill_continuous(guide = guide_legend()) 

我将不胜感激.反正我可以撤消订单吗?

谢谢


添加:默认情况下,我想为guide_legend添加该 reverse = FALSE .我认为 reverse = TRUE 将解决此问题,但我不了解重置默认值的意义.我做错什么了吗?

这里是指南:我指的是此图片来自上述网站.

解决方案

您的基本图具有连续的色标,您可以在图例中看到它:

  base<-ggplot(df,aes(x,y))+ geom_raster(aes(fill = z)) 

这会将其转换为离散的色标,因为设置 guide ="legend" guide = guide_legend()会创建离散的色标(这是

对于连续的色标,您可以使用下面的代码,但这不是必需的,因为当颜色变量是连续的(如您的 base 图中所示)时,ggplot在默认情况下已经这样做:

  base + scale_fill_continuous(guide = guide_colourbar()) 

保持离散的色标,但顺序相反,以便最高的值位于最上面.我还设置了休息时间,以使图例中仅显示3种颜色:

  base + scale_fill_continuous(breaks = 1:3,guide = guide_legend(reverse = TRUE)) 

更新:为了解决冗长的评论主题,这是

I am an absolute beginner in ggplot2 and practicing to use guide_legend() functions. I am not quite sure why guide_legend() reverses the order of the legend.

Here's the base example:

df <- data.frame(x = 1, y = 1:3, z = 1:3)
base <- ggplot(df, aes(x, y)) + geom_raster(aes(fill = z))

This nicely produces a raster graph.

However, when I add the following line, the order of legend gets reversed. Can someone please explain why this is happening?

base + scale_fill_continuous(guide = guide_legend())

I'd appreciate any thoughts. Is there anyway I can reverse the order?

Thanks


Addition: I want to add that reverse = FALSE by default for guide_legend. I think reverse=TRUE will fix it, but I am not understanding the point of resetting the default value. Is there anything wrong that I am doing?

Here's the guide: I am referring to http://docs.ggplot2.org/current/guide_legend.html

Here's the pic: This pic is from above website.

解决方案

Your base plot has a continuous color scale, which you can see in the legend:

base <- ggplot(df, aes(x, y)) + geom_raster(aes(fill = z))

This converts it to a discrete color scale, because setting guide="legend" or guide=guide_legend() creates a discrete scale (this is documented in the help):

base + scale_fill_continuous(guide = guide_legend())

For a continuous color scale, you could use the code below, but it's not necessary because that's already what ggplot does by default when the color variable is continuous (as shown in your base plot):

base + scale_fill_continuous(guide = guide_colourbar())

Keep discrete color scale, but reverse order so that highest value is on top. I've also set the breaks so that only 3 colors are displayed in the legend:

base + scale_fill_continuous(breaks=1:3, guide=guide_legend(reverse=TRUE))

UPDATE: To address the long comment thread, here are the three plots from the ggplot help for guide_legend. But note that the first one is not actually displayed on the ggplot help page; only the code is shown.

df <- reshape2::melt(outer(1:4, 1:4), varnames = c("X1", "X2"))

p1 = ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p2 = p1 + scale_fill_continuous(guide = "legend")
p3 = p1 + scale_fill_continuous(guide = guide_legend())

这篇关于使用guide_legend()反转图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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