Reputation: 25
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
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:
Upvotes: 1