查看警告消息中的行

See the line from warning message

当 ggplot 中出现警告消息时:

Removed 1 rows containing missing values (geom_point).

如何查看图表中排除了哪条线以了解如何解决问题?

使用 mtcars 作为示例数据,只需过滤包含绘图变量缺失的行:

library(tidyverse)

set.seed(42)

mtcars_na <- mtcars %>% 
  mutate(mpg = sample(c(mpg, rep(NA, 2)), length(mpg)))

ggplot(mtcars_na, aes(hp, mpg, color = cyl)) +
  geom_point()
#> Warning: Removed 2 rows containing missing values (geom_point).

mtcars_na %>% 
  # Add row id
  rowid_to_column() %>% 
  # filter for missings in one of the vars used in the plot
  filter(is.na(hp) | is.na(mpg) | is.na(cyl))
#>   rowid mpg cyl disp  hp drat   wt qsec vs am gear carb
#> 1    29  NA   8  351 264 4.22 3.17 14.5  0  1    5    4
#> 2    30  NA   6  145 175 3.62 2.77 15.5  0  1    5    6

reprex package (v0.3.0)

创建于 2020-03-31