library(tidyverse)
## -- Attaching packages --------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0 v purrr 0.3.3
## v tibble 2.1.3 v dplyr 0.8.3
## v tidyr 1.0.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## -- Conflicts ------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
dat <- data.frame(x=c("a1", "a2", "a3", "a2"),
y=c(1, 2, 3, 4))
boxplot(y ~ x, dat)
filtered_dat <- dat %>% filter(x != "a3")
filtered_dat
boxplot(y ~ x, filtered_dat)
Why do we see a3 on the horizontal axis?
filtered_dat$x
## [1] a1 a2 a2
## Levels: a1 a2 a3
Note that we have a3 in levels! It wasn’t removed on filtering.
levels(filtered_dat$x)
## [1] "a1" "a2" "a3"
We can remove these levels:
filtered_dat$x <- factor(filtered_dat$x)
levels(filtered_dat$x)
## [1] "a1" "a2"
Now we can make boxplot again:
boxplot(y ~ x, filtered_dat)
Another way is to use ggplot’s geom_boxplot instead of boxplot.
new_filtered_data <- dat %>% filter(x != "a3")
levels(new_filtered_data$x)
## [1] "a1" "a2" "a3"
new_filtered_data %>% ggplot(aes(x=x, y=y)) + geom_boxplot()