Null variable when plotting

Hi. I'm trying to create a plot with different variables. Here is what I have:

degree_factor <- factor(gss$degree, levels = c("BACHELOR", "GRADUATE","HIGH SCHOOL", "JUNIOR COLLEGE", "LT HIGH SCHOOL", "NA"))
levels(degree_factor)
gss_7 <- table(degree_factor)
barplot(gss_7, col="blue", xlab="Degree", ylab="Number", border="red", names.arg =c ("Bach.","Grad.","HS","Jun.C","Lt HS","NA"),ylim=c(0,30000)


When I'm plotting the null variable, the word "NA" doesn't work which gives me a 0. Can anyone help me with it? Thank you.

In gss$degree how many are NA?

1 Like

There are 142 of them are NA

I think they are plotted. It's just they don't look substantial given your y axis limit. Can you experiment with reducing it?

I'm pretty sure they are not because when I print the table it shows 0 that column

degree_factor
BACHELOR GRADUATE HIGH SCHOOL JUNIOR COLLEGE LT HIGH SCHOOL NA
7524 3613 27549 2902 11744 0

Edit: my first thought in this post is wrong, please see my example in the post below it


Ah, thanks. It's probably this issue...

https://www.stat.berkeley.edu/~s133/factors.html
"To exclude certain levels from appearing in a factor, the exclude= argument can be passed to factor. By default, the missing value (NA) is excluded from factor levels; to create a factor that inludes missing values from a numeric variable, use exclude=NULL"

sorry, I was wrong. I think the thing to do, is not bother with naming NA factor level, just name your other levels, when you go to do the table though, use the helper function addNA

charvec <- c(rep("a",3),
             rep("b",4),
             rep(NA,5))
factorvec <- factor(charvec, levels=c("a","b")   )

levels(factorvec)

fvt <- table(addNA(factorvec , ifany=TRUE))
fvt
barplot(fvt, col="blue",
        xlab="Degree",
        ylab="Number",
        border="red", 
        names.arg =c ("a","b","NA"))![Rplotexample|478x477]

Rplotexample

Thank you so much! It makes sense and works perfectly.

1 Like

You're welcome, thanks for marking a solution

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.