简体   繁体   中英

Remove NA from plot in R

I have the following table that I want to plot in R

     A    B    C  NA
0   500  200  200 0

This table is generated from a variable. I had previously removed the NAs using data<-data[!(data$pid3==""),]

If I use the plot() function with this variable, the NA and "" shows up in the plot. How do I get rid of this in the plot?

Thank you!

There is a difference between "" and NA in R:

> is.na("")
[1] FALSE
> is.na(NA)
[1] TRUE

If you want to remove NAs, you should use something like this:

data <- data[!is.na(data$pid3),]

(It may be a good idea to remove empty strings as well, so you can run the command above in addition to your previous filtering step.)

I figured it out. I was able to achieve this using the ggplot2 package.

I generate the table using

table <- data %>% group_by(pid3) %>% summarise(n = n())

Then I plot using ggplot2

ggplot(table, aes(x = pid3, y = n)) +
  geom_bar(stat="identity", position=position_dodge()) + theme_classic()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM