如何从图例中省略因子的NA
水平?
从nycflights13
数据库中,我创建了一个名为tot_delay
的新连续变量,然后创建了一个名为delay_class
的因子,该因子具有4个水平。当我绘图时,我过滤掉了NA
值,但它们仍然显示在图例中。下面是我的代码:
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")
4条答案
按热度按时间af7jpaap1#
父级示例并不能很好地说明问题(当然,应该跟踪并消除意外的
NA
值),但这是Google上的顶级结果,因此应该注意的是,scale_XXX_XXX
中现在有一个选项,可以通过设置na.translate = F
来防止NA级别显示在图例中。例如:这在ggplot2 3.1.0中有效。
j1dl9f462#
你有一个数据点,其中
delay_class
是NA
,但tot_delay
不是。这个点没有被过滤器捕获。将代码更改为:就能做到
或者,如果您绝对必须有额外的点,您可以覆盖
fill
图例,如下所示:**更新:**正如@gatsky的回答所指出的,所有离散尺度都包含
na.translate
参数。该功能实际上从ggplot 2.2.0开始就存在了;我只是在发布我的答案时没有意识到它。为了完整起见,它在原始问题中的用法如下:xesrikrc3#
我喜欢上面@阿尔特姆的方法,也就是说,找出df中存在NA的原因。然而,有时候你“知道”存在NA,而你只是想排除它们。在这种情况下,简单地使用'na.omit'就可以了:
watbbzwu4#
如果你知道
NA
的颜色是grey50
,你也可以使用scale_color_manual
或scale_fill_manual
。所以你可以指定values
,只指定breaks
来绘制你想要绘制的颜色。下面是一些可重复的代码(data @gatsky):正如您所看到的,通过指定
values
中的所有颜色,只有A和B的breaks
将从图例中删除NA。如果你想在图例中使用标准颜色,你可以使用
scales
包中的hue_pal
函数,如下所示:最后,使用OP中的数据,以
scale_fill_manual
为例,如下所示:创建于2023-03-25带有reprex v2.0.2