本文介绍了ggplot:删除图例中的NA因子水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从图例中省略因子的NA水平?

How can I omit the NA level of a factor from a legend?

nycflights13数据库中,我创建了一个名为tot_delay的新连续变量,然后创建了一个具有4个级别的名为delay_class的因数.绘制时,我会滤除NA值,但它们仍会出现在图例中.这是我的代码:

From the nycflights13 database, I created a new continuous variable called tot_delay, and then created a factor called delay_class with 4 levels. When I plot, I filter out NA values, but they still appear in the legend. Here's my code:

library(nycflights13); library(ggplot2)

flights$tot_delay = flights$dep_delay + flights$arr_delay
flights$delay_class <- cut(flights$tot_delay,
                           c(min(flights$tot_delay, na.rm = TRUE), 0, 20 , 120,
                             max(flights$tot_delay, na.rm = TRUE)),
                           labels = c("none", "short","medium","long"))

filter(flights, !is.na(tot_delay)) %>%
  ggplot() +
  geom_bar(mapping = aes(x = carrier, fill = delay_class), position = "fill")

推荐答案

您有一个数据点,其中delay_classNA,而tot_delay不是.这一点不会被您的过滤器捕获.将您的代码更改为:

You have one data point where delay_class is NA, but tot_delay isn't. This point is not being caught by your filter. Changing your code to:

filter(flights, !is.na(delay_class)) %>%
  ggplot() +
  geom_bar(mapping = aes(x = carrier, fill = delay_class), position = "fill")

做到了:

或者,如果您绝对必须有这个额外的要点,则可以按以下方式覆盖fill图例:

Alternatively, if you absolutely must have that extra point, you can override the fill legend as follows:

filter(flights, !is.na(tot_delay)) %>%
  ggplot() +
  geom_bar(mapping = aes(x = carrier, fill = delay_class), position = "fill") +
  scale_fill_manual( breaks = c("none","short","medium","long"),
                    values = scales::hue_pal()(4) )

更新:正如@gatsky的答案中指出的那样,所有离散量表也都包含na.translate参数.自 ggplot 2.2.0 以来,该功能实际上就存在.发布答案时,我只是不知道它.为了完整起见,它在原始问题中的用法看起来像是

UPDATE: As pointed out in @gatsky's answer, all discrete scales also include the na.translate argument. The feature actually existed since ggplot 2.2.0; I just wasn't aware of it at the time I posted my answer. For completeness, its usage in the original question would look like

filter(flights, !is.na(tot_delay)) %>%
  ggplot() +
  geom_bar(mapping = aes(x = carrier, fill = delay_class), position = "fill") +
  scale_fill_discrete(na.translate=FALSE)

这篇关于ggplot:删除图例中的NA因子水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:13