Create paired boxplots, from 2 distinct dataframes

2019-06-02 04:45发布

问题:

I would like to create a graphic with box plot in R. I got the following data frames:

> drools_responseTimes_numberOfClients_REST
      X1   X5  X10  X20   X50
1    816  183  699  154   297
2    366  280 1283  345   291
3    103  946 1609  409   377
4    431 1086 1974  482   479
5     90 1379 2083  567   557
6    290  511 2184  910   925
7    134  770 2283  980  1277
8    480 1547 2416 1069  1752
9    275 1727 2520 1141  1846
10    67  679 2616 1188  1935

> javascript_responseTimes_numberOfClients_REST
       X1   X5  X10   X20   X50
1     334  497  610   439   417
2     445  894  859   826   588
3     306 1143 1123  1407   791
4     301 1442 1445  1806  1005
5     257 1754 1857  2209  1235
6     181  507 2078  2493  1441
7     436 1186 2419  2885  1677
8     353 2280 2708  3101  1909
9     350 2984 2997  3358  2106
10    296  544 3185  3817  2353

I want to create paired box plot for each column distinguishing the type by the color as shown here: https://stackoverflow.com/a/17922219/3503168

回答1:

I suggest a solution using ggplot+reshape2, with random data:

set.seed(10)
drools <- data.frame(matrix(round(runif(50, 50, 1000)), ncol=5))
java <- data.frame(matrix(round(runif(50, 50, 1000)), ncol=5))

library(ggplot2)
library(reshape2)

df <- data.frame(melt(drools), melt(java)[2])
names(df) <- c("column", "drools", "java")
df2 <- melt(df)

ggplot(data=df2) +
  geom_boxplot(aes(x=column, y=value, fill=variable))

It gives:

What 'melt' actually does is transform the 5 (then 2 for the second use) columns into factors inside one single colum, which makes it easy for plotting.