1

EDIT: Added the boxplot generated with standard boxplot() function.

Given the iris dataste, the following code:

boxplot(iris[,])

Creates a boxplot with five boxes, one for each variable, without splitting them into categories such as, for instance, species. While this is simple enough, I have been unable to do the same in ggplot2.

Boxplot generated with standard function.

My question, then, is simple: how can I achieve this?

Danks C.
  • 73
  • 2
  • 8

1 Answers1

3

Species is a factor with three levels (setosa, versicolor and virginica). I think it doesn't make sense if you plot it with the other variables.

It makes more sense if you want to plot all other 4 variables (Sepal.Length, Sepal.Width, Petal.Length, and Petal.Width) in one plot as below

library(dplyr)
library(tidyr)
library(ggplot2)
iris %>% dplyr::select(Species, everything()) %>% tidyr::gather("id", "value",2:5) %>% 
  ggplot(., aes(x = id, y = value))+geom_boxplot()

enter image description here

If you want to plot all 5 variables in the same plot, you need to convert species to be numeric

iris %>% dplyr::mutate(Species = as.numeric(Species)) %>% tidyr::gather("id", "value",1:5) %>% 
  ggplot(., aes(x = id, y = value))+geom_boxplot()

enter image description here

shiny
  • 3,380
  • 9
  • 42
  • 79