Home > Mobile >  How can I exclude NA values when visualizing data in R?
How can I exclude NA values when visualizing data in R?

Time:06-30

I'm trying to use ggplot to visualize the data in R using a bar chart. I have NA values that I want to ignore without deleting any rows or columns. I tried using na.rm = TRUE but it is not working. Is this correct? If not how can I do it?

ggplot(data, aes(x = Flavours, na.rm = TRUE))  
  geom_bar()   labs(y = "Here is a label", title = "Here is the title" )  
  theme_bw()

CodePudding user response:

Try with this filter in your data.

The != symbol it is used to mute rows with hsa NAs as a condition

ggplot(data %>% filter(!is.na(Flavours), aes(x = Flavours))   
geom_bar()   
labs(y = "Here is a label", title = "Here is the title" )   
theme_bw()

To use the pipe %>% will need install the package dplyr

CodePudding user response:

Here you have an example following your original code:

library(tidyverse)
data <- data.frame(x = c("A","B","C","D",NA, "F", "G", NA, NA, NA, NA, NA,"L"),
                   y = c(NA, 10, 12, NA, 32, 21, NA, 35, NA, 32, 21, NA, 35))


ggplot(data, aes(y, na.rm = TRUE))  
  geom_bar()   labs(y = "Here is a label", title = "Here is the title" )  
  theme_bw()

OUTPUT: enter image description here

  •  Tags:  
  • r
  • Related