繁体   English   中英

R:如何在同一ggplot上绘制多个箱形图,每个箱形图来自数据集的单个列?

[英]R: How to plot multiple boxplots, each from a single column in a dataset, on the same ggplot?

我有以下这种结构的数据表:

+-----+-------+-------+-------+
| key | col1  | col2  | col3  |
+-----+-------+-------+-------+
| A   | 1000  | 56    | 1     |
| A   | 2000  | 3     | NaN   |
| B   | 2001  | 23    | 90    |
| A   | 2002  | 87    | 42    |
| A   | 2004  | 12    | 12    |
| B   | 2002  | 1     | NaN   |
| C   | 2002  | 3     | 14    |
+-----+-------+-------+-------+

我的目标是为每个键绘制多个箱形图的1个ggplot,其中ggplot中的每个boxplot是数据集中每个列的可视化。 反正有实现这一目标的方法吗?

据我了解

 df <- structure(list(key = structure(c(1L, 1L, 2L, 1L, 1L, 2L, 3L), .Label = c("A", 
    "B", "C"), class = "factor"), col1 = c(1000L, 2000L, 2001L, 2002L, 
    2004L, 2002L, 2002L), col2 = c(56L, 3L, 23L, 87L, 12L, 1L, 3L
    ), col3 = c(1, NaN, 90, 42, 12, NaN, 14)), .Names = c("key", 
    "col1", "col2", "col3"), class = "data.frame", row.names = c(NA, 
    -7L))

library(reshape2)
df1 <- melt(df)

library(ggplot2)
ggplot(aes(x = variable, y = value), data = df1) + geom_boxplot() + facet_wrap(~key)

在此处输入图片说明

要么

ggplot(aes(x = variable, y = value), data = df1) + geom_boxplot() + facet_wrap(variable~key)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM