nan
nan

Reputation: 1191

Plot box-whisker plot for each column individually in a single plot (ggplot2)

I have a dataset of 10000 rows and 5 columns. One of the columns defines the type of the data (variable of factor type).

The data is like this:

V1  V2      V3    V4     V5    type
1.0  2.3    2.3   4.4    5.6   "1"
0.4  3.1    6.2   5.5    5.8   "2"
1.2  2.2    7.2   4.8    5.9   "3"
 ........

By running the following command:

p <- ggplot(dataframe, aes(type,V1))
p + geom_boxplot()

enter image description here

However, I want to do it for all the five variables, and display the results in parallel in a single graph (e.g. stack the plots one above another). How can I do it with ggplot2 in R?

Upvotes: 0

Views: 359

Answers (2)

nan
nan

Reputation: 1191

Hi thanks for the answer, but i found a different solution.

I run this:

 require(gridExtra)
 p1 <- qplot(type, V1, data=dataframe, geom="boxplot")
 p2 <- qplot(type, V2, data=dataframe, geom="boxplot")
 p3 <- qplot(type, V3, data=dataframe, geom="boxplot")
 p4 <- qplot(type, V4, data=dataframe, geom="boxplot")
 p5 <- qplot(type, V5, data=dataframe, geom="boxplot")
 grid.arrange(p1,p2,p3,p4,p5)

This implements what I want without casting the dataframe to long format.

Upvotes: 0

Didzis Elferts
Didzis Elferts

Reputation: 98439

You should reshape your data to long format and then use faceting.

library(reshape2)
df.long<-melt(dataframe,id.vars="type")
ggplot(df.long,aes(as.factor(type),value))+geom_boxplot()+
      facet_grid(variable~.)

Upvotes: 4

Related Questions