vilsef
vilsef

Reputation: 17

Boxplot with variables of different length

I'm plotting a boxplot with two variables:

ggplot(df, aes(y=lvl_edu, x=aia_index)) + geom_boxplot()

But the plot is displaying the NA values for my Y variable, which I do not want. When I eliminate the NA's (na.omit(y), R gives me the error: Error: Aesthetics must be either length 1 or the same as the data.

Anyone knows how to fix this problem? Attaching a pic of the plot with the NA showing.

enter image description here

Thanks in advance!

Upvotes: 0

Views: 147

Answers (1)

GuedesBF
GuedesBF

Reputation: 9858

Try to remove the NAs before the data enters the ggplot function call.

library(dplyr)
library(ggplot2)

df %>% filter(!is.na(lvl_edu)) %>% #preliminary filter to remove NAs
    ggplot(., aes(y=lvl_edu, x=aia_index))+ #same as original code from here
    geom_boxplot()

We can also call na.omit inside the data argument to ggplot. As in:

library(ggplot2)

ggplot(df[na.omit(df$lvl_edu),], aes(y=lvl_edu, x=aia_index)) + geom_boxplot()

Upvotes: 0

Related Questions