Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminating NAs from a ggplot

Tags:

r

ggplot2

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?

like image 497
Ben Eichler Avatar asked Jun 20 '13 14:06

Ben Eichler


People also ask

Does ggplot remove missing values?

rm = FALSE . ggplot is somewhat more accommodating of missing values than R generally. For those stats which require complete data, missing values will be automatically removed with a warning.

Is ggplot and ggplot2 the same?

You may notice that we sometimes reference 'ggplot2' and sometimes 'ggplot'. To clarify, 'ggplot2' is the name of the most recent version of the package. However, any time we call the function itself, it's just called 'ggplot'.

Can you filter within ggplot?

ggplot2 allows you to do data manipulation, such as filtering or slicing, within the data argument.

Can you subset in ggplot?

Method 1: Using subset() function Here, we use subset() function for plotting only subset of DataFrame inside ggplot() function inplace of data DataFrame. All other things are same. Parameters: It takes data object to be subsetted as it's first parameter.


1 Answers

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") 
like image 140
rafa.pereira Avatar answered Sep 28 '22 11:09

rafa.pereira