使用ggplot绘制无NA值柱状图遇异常,求正确实现方法
问题描述
给定数据集:
structure(list(sex = structure(c(2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L), levels = c("Male", "Female"), class = "factor"), ph006d1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c("238834", "140684", "313895", "357343", "395260", "113302", "261429", "56770", "246735", "44335", "243570", "291491", "169951", "84282", "128746", "370073", "184552", "142856", "159969", "222004", "214571", "206412", "9604", "408052", "247889", "160392", "112151", "294933", "60628", "255761", "183798", "355557", "382694", "179322", "232143", "8384", "64595", "130937", "58238", "211288", "89741", "274124", "15498", "263105", "16023", "294046", "158389", "194203", "249063", "263042", "269633", "273931", "334222", "307707", "143820", "394474", "170366", "77730", "263558", "215557", "108658", "280293", "153393", "19683", "76186", "398330", "291272", "381430", "7460", "5354", "319521", "132904", "222158", "34189", "403611", "206023", "37337", "70288", "371791", "46469", "307796", "196647", "145693", "181532", "152799", "298512", "325908", "410652", "315940", "224851", "109837", "339716", "94379", "85190", "92170", "308636", "332581", "344942", "15515", "406412"), class = "data.frame")
尝试绘制ph006d1列0和1的统计柱状图,首次使用代码:
ggplot(datasample,aes(x=ph006d1))+geom_bar(stat="count")
出现错误:
Error: (converted from warning) Removed 1 rows containing non-finite values (stat_count)
改用代码:
ggplot(datasample,aes(x=factor(ph006d1)))+geom_bar(stat="count")
绘图成功,但出现NA分组;然而执行table(datasample$ph006d1)仅返回:
0 1 91 8
原因分析
table()函数默认会忽略NA值,所以输出里看不到NA的统计结果,但数据集里确实存在1个NA值(首次报错提示"Removed 1 rows"也能佐证)。- 将数值型的
ph006d1转换为factor时,NA会被自动识别为一个独立的因子水平,因此ggplot会将其作为一个分组绘制出来。 - 首次绘图报错是因为
ph006d1是数值型,stat_count处理非数值(NA)时会移除对应行,触发警告转错误。
正确绘图方法
提供以下几种可行方案:
方案1:先过滤NA值再绘图
直接从数据集中移除包含NA的行,再进行绘图:
library(ggplot2) library(dplyr) # 过滤NA行 datasample_clean <- datasample %>% filter(!is.na(ph006d1)) # 绘制柱状图 ggplot(datasample_clean, aes(x = factor(ph006d1))) + geom_bar(stat = "count")
方案2:转换因子时排除NA
在factor()函数中指定exclude = NA,直接把NA排除在因子水平外:
ggplot(datasample, aes(x = factor(ph006d1, exclude = NA))) + geom_bar(stat = "count")
方案3:在ggplot中隐藏NA分组
如果不想修改原始数据,可以通过scale_x_discrete()的drop参数去掉NA分组:
ggplot(datasample, aes(x = factor(ph006d1))) + geom_bar(stat = "count") + scale_x_discrete(drop = TRUE)
方案4:使用drop_na()快速清理数据
利用tibble包的drop_na()函数一键移除含NA的行:
library(ggplot2) library(tibble) ggplot(drop_na(datasample, ph006d1), aes(x = factor(ph006d1))) + geom_bar(stat = "count")
内容的提问来源于stack exchange,提问作者user3483060
相关产品推荐
相关产品推荐

