How to remove NA from a factor variable (and from a ggplot chart)?

前端 未结 3 1802
深忆病人
深忆病人 2021-01-14 03:33

I have a problem with NA in a factor variable since ggplot includes them in the plot as if they are another category/level. I would like to drop the missing data. I am sorry

相关标签:
3条回答
  • 2021-01-14 04:11

    Answers on this related thread: NA's are being plotted in boxplot ggplot2

    In brief, instead of the usual:

    ggplot(data=data)
    

    use

    ggplot(data=na.omit(data[,c("var1","var2",...)])) 
    

    where var1, var2 etc are the variables you are plotting.

    0 讨论(0)
  • 2021-01-14 04:23

    I'd use qplot instead of ggplot in this way:

    qplot(x=column, data=subset(dataframe,!is.na(column)))
    

    I hope this helps.

    0 讨论(0)
  • 2021-01-14 04:24

    assuming your data is in a data frame called dat

    newdat <- dat[!is.na(dat$Factor), ]

    not sure how to solve the problem inside of ggplot code

    0 讨论(0)
提交回复
热议问题