不要用 ggplot2 绘制一些计数

Do not plot some counts with ggplot2

作为可重现的例子,使用

library(tidyverse)
iris_count <- count(iris, Species)

iris_count %>%
  mutate(Species2 = ifelse(Species == "setosa", NA, as.character(Species))) %>%
  ggplot(aes(reorder(Species2, -n), n)) +
  geom_col(na.rm = TRUE)

我想从情节中删除 NA,但在这种情况下选项 na.rm = TRUE 似乎无法满足我的要求。

使用 scale_x_discrete 和参数 na.translate = FALSE。来自 scale_x_discrete 文档:

na.translate Unlike continuous scales, discrete scales can easily show missing values, and do so by default. If you want to remove missing values from a discrete scale, specify na.translate = FALSE.

library(ggplot2)
library(dplyr)
iris_count <- count(iris, Species)

iris_count %>%
  mutate(Species2 = ifelse(Species == "setosa", NA, as.character(Species))) %>%
  ggplot(aes(reorder(Species2, -n), n)) +
  geom_col() + 
  scale_x_discrete(na.translate = FALSE)