user14290669
user14290669

Reputation: 25

Boxplots in a single graph

In ggplot, how to create boxplots in a single graph by group.If i have a data

data(iris)
library(ggplot2)
ggplot(iris) + geom_boxplot(Sepal.Length)

In this way, I know to create the boxplots for other columns. How to group and plot simultaneously in a singleplot

Upvotes: 1

Views: 43

Answers (1)

Duck
Duck

Reputation: 39605

Maybe this. You have to reshape your data to long and using a variable as reference. In the case of this example Species. Then use common aesthetics elements for axis and fill options and the plot will be done. Here the code:

library(ggplot2)
library(tidyverse)
#Code
iris %>% pivot_longer(-Species) %>%
    ggplot(aes(x=Species,y=value,fill=name))+
    geom_boxplot()

Output:

enter image description here

Upvotes: 1

Related Questions