从ggplot中消除NAs [英] Eliminating NAs from a ggplot
问题描述
这里非常基本的问题在于我刚刚开始使用R,但我试图在ggplot2中创建一个因子计数的条形图,并在绘图时获取14个代表我实际水平的小彩色闪烁,然后显示一个巨大的灰色最后代表样本中的5000个新生样本(这是一个仅适用于样本约5%的问题的调查数据)。我试过以下代码无济于事:
ggplot(data = MyData,aes(x = the_variable,fill = the_variable ,na.rm = TRUE))+
geom_bar(stat =bin)
此处添加的na.rm参数没有明显的效果。
同时
ggplot(data = na.omit(MyData),aes(x = the_variable,fill = the_variable,na.rm = TRUE))+
geom_bar(stat =bin)
给我
错误:美学必须是长度为1或与数据长度相同的长度
与添加 na相同.omit()
添加到the_variable,或MyData和the_variable。
所有我想要做的就是从我的图表中消除巨大的NA栏,有人可以帮我做这个吗?
子集
函数。试试这个 library(ggplot2)
data(iris)
iris $ Sepal.Length [5:10]< - NA#为这个例子创建了一些NA
ggplot(data = subset(iris,!is.na(Sepal.Length)),aes(x = Sepal.Length))+
geom_bar(stat =bin)
Very basic question here as I'm just starting to use R, but I'm trying to create a bar plot of factor counts in ggplot2 and when plotting, get 14 little colored blips representing my actual levels and then a massive grey bar at the end representing the 5000-ish NAs in the sample (it's survey data from a question that only applies to about 5% of the sample). I've tried the following code to no avail:
ggplot(data = MyData,aes(x= the_variable, fill=the_variable, na.rm = TRUE)) +
geom_bar(stat="bin")
The addition of the na.rm argument here has no apparent effect.
meanwhile
ggplot(data = na.omit(MyData),aes(x= the_variable, fill=the_variable, na.rm = TRUE)) +
geom_bar(stat="bin")
gives me
"Error: Aesthetics must either be length one, or the same length as the data"
as does affixing the na.omit()
to the_variable, or both MyData and the_variable.
All I want to do is eliminate the giant NA bar from my graph, can someone please help me do this?
You can use the function subset
inside ggplot2
. Try this
library(ggplot2)
data("iris")
iris$Sepal.Length[5:10] <- NA # create some NAs for this example
ggplot(data=subset(iris, !is.na(Sepal.Length)), aes(x=Sepal.Length)) +
geom_bar(stat="bin")
这篇关于从ggplot中消除NAs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!