删除R中的ggplot()检测到的缺失值 [英] Delete missing values detected by ggplot() in R

查看:225
本文介绍了删除R中的ggplot()检测到的缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了这个问题,以了解如何绘制许多图同一图中的图形.按照我喜欢并接受的答案,可以使用 ggplot()函数.

现在使用 ggplot(),我收到以下消息,通知您已删除该图的缺失值:

 警告消息:删除了33个包含缺失值的行(geom_path). 

从生成的图上进行可视化处理后,我对数据感到满意,因为 ggplot()删除了33行.

我知道如何删除NA行,但是在这里我不明白 ggplot()是否删除了至少存在一个变量 OR 的行,而删除了行在知道我有7个变量的情况下,所有变量都是NA,并且有些行中所有变量都是NA,而许多行只包含某些变量的NA.

问题:尽管对于该图已经删除了行,但是如何在检测到的33行"中删除这些行是可能的.完全来自数据?

解决方案

ggplot 删除具有NA的行,这些行用作 ggplot ,如果输入是x和y列,但数据框也具有y列,则仅当x或y具有NA时,才会删除行.

这里是一个例子:

 库(ggplot2)x<-头(mtcars)#将NA添加到我们不用于ggplot的某些列中x $ am [1]<-不适用ggplot(x,aes(cyl,mpg))+ geom_point()#没有警告#现在将NA添加到用于绘图的列x $ cyl [1]<-不适用ggplot(x,aes(cyl,mpg))+ geom_point()# 警告信息:#删除了1个包含缺失值的行(geom_point).#为避免该警告,我们可以将其明确设置为删除不适用ggplot(x,aes(cyl,mpg))+ geom_point(na.rm = TRUE)#没有警告 

要从数据中删除行,请检查所选列是否具有NA:

  x_clean<-x [!(is.na(x $ cyl)| is.na(x $ mpg)),]ggplot(x_clean,aes(cyl,mpg))+ geom_point()#没有警告 


要基于注释应用数据,请尝试以下操作,请参见过滤器:

 数据<-bind_rows(...)数据%>%mutate(data = paste0('Data',data))%&%ivot_longer(-c(数据,时间))%&%;%filter(!(is.na(Time)| is.na(value)))%&%ggplot(aes(x = factor(Time),y = value),group = name,color = name))+geom_line()+facet_wrap(.〜data,scales ='free',ncol = 1)+xlab('时间') 

了解"哪些数据要进入ggplot,为什么不将过滤后的干净数据作为单独的对象而不是管道进行保存,请参阅:

 数据<-bind_rows(...)cleanData<-数据%>%mutate(data = paste0('Data',data))%&%ivot_longer(-c(数据,时间))%&%;%filter(!(is.na(Time)| is.na(value)))ggplot(cleanData,aes(x = factor(Time),y = value),group =名称,color =名称)+geom_line()+facet_wrap(.〜data,scales ='free',ncol = 1)+xlab('时间') 

I asked this question to know how it is possible to plot many graphs in the same plot. Following to the answer which I liked and accepted, it is possible to use ggplot() function.

Now using ggplot(), I receive the following message which notifies that there are missing values were deleted for the plot:

Warning message:
Removed 33 row(s) containing missing values (geom_path).

From the produced plot and visualizing, I am satisfied with data after that ggplot() removed the 33 rows.

I know how to delete rows of NA but here I don't understand if ggplot() deleted rows where there exist NA for at least one variable OR removed rows where all variables are NA, knowing that I have 7 variables and there are some rows where all variables are completely NA while many rows contain NA for only some variables.

Question: Although the rows are already deleted for the plot, how it is possible to remove these rows "the detected 33 rows" completely from data?

解决方案

ggplot removes rows with NA for columns that are used as input aes to ggplot, if input is x and y columns, but dataframe has y column as well, it will only drop rows if x or y has NA.

Here is an example:

library(ggplot2)

x <- head(mtcars)

# add NA to some column we don't use for ggplot
x$am[ 1 ] <- NA

ggplot(x, aes(cyl, mpg)) + geom_point()
# no warnings

# now add NA to column that we use for plotting
x$cyl[ 1 ] <- NA

ggplot(x, aes(cyl, mpg)) + geom_point()
# Warning message:
#   Removed 1 rows containing missing values (geom_point). 

# to avoid that warning, we can explicitly set it to remove NA
ggplot(x, aes(cyl, mpg)) + geom_point(na.rm = TRUE)
# no warnings

To remove rows from the data, check if the selected columns have NA:

x_clean <- x[ !(is.na(x$cyl) | is.na(x$mpg)), ]
ggplot(x_clean , aes(cyl, mpg)) + geom_point()
# no warnings


Edit 1: To apply to your data based on comments, try below, see filter:

Data <- bind_rows(...)
Data %>%
  mutate(data = paste0('Data',data)) %>%
  pivot_longer(-c(data,Time)) %>%
  filter(!(is.na(Time) | is.na(value))) %>% 
  ggplot(aes(x = factor(Time), y =value), group = name, color = name))+
  geom_line()+
  facet_wrap(.~data,scales = 'free', ncol = 1) +
  xlab('Time')

Edit 2: To "know" what data is going into ggplot why not keep filtered clean data as a separate object instead of piping, see:

Data <- bind_rows(...)
cleanData <- Data %>% 
  mutate(data = paste0('Data',data)) %>%
  pivot_longer(-c(data,Time)) %>%
  filter(!(is.na(Time) | is.na(value)))
  
ggplot(cleanData, aes(x = factor(Time), y =value), group = name, color = name)+
  geom_line()+
  facet_wrap(.~data,scales = 'free', ncol = 1) +
  xlab('Time')

这篇关于删除R中的ggplot()检测到的缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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