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?
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.
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'.
ggplot2 allows you to do data manipulation, such as filtering or slicing, within the data argument.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With