Grouped Boxplots

Deadly 提交于 2021-02-05 08:21:25

问题


I have data frame and I want to plot grouped boxplots. Here is part of the data.

a <- c(0, 100, 250, 500, 750, 0, 100, 250, 500, 750, 0, 100, 250, 500, 750)
b <- c(2.646, 3.756, 26.270, 30.997, 39.294,  2.646, 3.756, 26.270, 30.997, 39.294, 16.363, 25.950, 38.913,45.000, 47.698)
c <- c(0.0, 0.5, 0.1, 5.8, 21.9, 0.0, 0.5, 0.1, 5.8, 21.9, 9.7. 12.5, 25.1, 29.3, 31.9)
d <- c(14.7, 15.0, 13.8, 18.4, 28.2, 14.7, 15.0, 13.8, 18.4, 28.2, 23.6, 24.0, 25.7, 29.0, 33.1)

I merged the vectors to get a data frame as follows.

data <- cbind(a, b, c, d)

Then I tried to get boxplot for the three values of b, c and d as a funtion of a as follows.

boxplot(b~a, c~a, d~a, data = data)

but I couldn't get the desired grouped boxplot which is three boxplots for each value of "a". But I can get a boxplot for each value of "a" if I do:

boxplot(b~a, data = data)

What is wrong with my grouped boxplot?


回答1:


First create a dataset in long format rather than a wide format.

wide <- data.frame(
  a = c(0, 100, 250, 500, 750, 0, 100, 250, 500, 750, 0, 100, 250, 500, 750),
  b = c(2.646, 3.756, 26.270, 30.997, 39.294,  2.646, 3.756, 26.270, 30.997, 39.294, 16.363, 25.950, 38.913,45.000, 47.698),
  c = c(0.0, 0.5, 0.1, 5.8, 21.9, 0.0, 0.5, 0.1, 5.8, 21.9, 9.7, 12.5, 25.1, 29.3, 31.9),
  d = c(14.7, 15.0, 13.8, 18.4, 28.2, 14.7, 15.0, 13.8, 18.4, 28.2, 23.6, 24.0, 25.7, 29.0, 33.1)
)
library(reshape2)
long <- melt(wide, id.vars = "a")

Then convert a into a factor you you can use it to define groups

long$a <- factor(long$a)

Finally you can plot this with ggplot2

library(ggplot2)
ggplot(long, aes(x = a, y = value, colour = variable)) + geom_boxplot()




回答2:


This is basically the solution by Thierry, but using boxplot(long$value ~ long$a + long$variable) instead of ggplot, once you have converted to long with melt() and factorized a. It is a different grouping though.



来源:https://stackoverflow.com/questions/37527236/grouped-boxplots

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