0

I am using R to analyze a data set that includes questions about income and one of the possible answers was "choose not to answer".

I was able to subset the data so that I could omit the "choose not to answer" by setting it equal to NA.

How can I remove the NAs so that it does not show up in my plots? Below is a copy of my data...

Below is a copy of my code. I have omitted "NAs", which I would like to remove "N/A", "Don't know" and "Refused". When I look at my new table (xA) I see them removed. When I look at the plot I get from the code below, the labels are still there but just with a value of 0. Note I had to use different variables for the purpose of this example.

`# set up data so no NAs`
`xA <- na.omit(x)`

`xtab17 <- table(xA$email,xA$education)`
`xtab18 <- as.data.frame.table(xtab17); xtab18`

`barplot(xtab17,`
    `ylim = c(0,600),`
    `legend = rownames(xtab17),`
    `main = "Still shows NAs"`
    `)`

Plot image here

macmet
  • 1
  • 1
  • 1
    Welcome to SO. Perhaps you can share your code so that someone can help you. Please see here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – YBS Oct 06 '20 at 00:57
  • Run `df <- droplevels(df)` and then plot, see if that helps. – Ronak Shah Oct 06 '20 at 01:16
  • did you check to see if `levels(xA$email)` and `levels(xA$education)` contain `NA` as a category? Also, inspect `xtab17 ` to see if this table contains `NA`. – KM_83 Oct 06 '20 at 19:45
  • Hi Everyone, It seems like everyone here is a little more advanced than I am and I was not able to get any of these to work. However, I did the plot to eventually turn out. I used "na.rm=T" in my logic and I am all good now. Thanks for the help everyone. – macmet Oct 08 '20 at 13:20

1 Answers1

0

Try filtering the data and plot it.


library(dplyr)
library(ggplot2)
df_iris <- iris
df_iris$Species[1:10] <- NA
df_iris %>% # this will show NA as a category
  ggplot(aes(x=Species, y =Sepal.Length)) + geom_boxplot()

df_iris %>%
  filter(!is.na(Species)) %>% # this will NOT show NA as a category
  ggplot(aes(x=Species, y =Sepal.Length)) + geom_boxplot()


KM_83
  • 697
  • 3
  • 9
  • Also, if it is a factor/categorical variable, check levels, for example `levels(df$var)` , and redefine the levels with `factor(..., levels = ...)` as needed. – KM_83 Oct 06 '20 at 01:10