Create paired boxplots, from 2 distinct dataframes

泄露秘密 提交于 2019-12-06 10:50:37

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!