摆脱NA数据点以创建箱线图

时间:2020-05-21 04:08:13

标签: r boxplot na missing-data graphing

我正在尝试摆脱这套并排箱形图的幻影箱形图。我知道这是由于缺少性别数据造成的,但是,我无法弄清楚我在代码!is.na(ny$Gender)中插入的位置。 这是我的代码和图形:

boxplot(ny$age ~ ny$Gender, col="orange", main="Distribution of age
                 and gender", ylab="Number of Users", xlab="Gender")

enter image description here

1 个答案:

答案 0 :(得分:0)

您似乎在Gender列下的数据框中有空白,这就是为什么要获得一个额外的框的原因。

请参阅我的可复制示例,以了解问题所在以及如何消除该问题。在此示例中,activ代表Gendertime代表age

attach(beaver2)

#View data
head(df)
> head(beaver2)
  day time  temp activ
1 307  930 36.58     0
2 307  940 36.73     0
3 307  950 36.93     0
4 307 1000 37.15     0
5 307 1010 37.23     0
6 307 1020 37.24     0

#rename the dataframe df
df <- beaver2

#introduce five blank spaces in the activ column to match your data 
 df[1:5,4] <- ""

#Visualize new dataframe
head(df)
  day time  temp activ
1 307  930 36.58      
2 307  940 36.73      
3 307  950 36.93      
4 307 1000 37.15      
5 307 1010 37.23      
6 307 1020 37.24     0

#Create boxplot with dataframe with blank spaces 

boxplot(df$time ~ df$activ, col="orange", main="Distribution of age 
and gender", ylab="Number of Users", xlab="Gender")

enter image description here

消除空格后,您将获得一个正常外观的箱线图

df$activ[which(df$activ=="")] <- NA

#create new boxplot
boxplot(df$time ~ df$activ, col="orange", main="Distribution of age
             and gender", ylab="Number of Users", xlab="Gender")

enter image description here

相关问题