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

时间:2021-09-16 13:02:11

I have the following data table of this structure:

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

+-----+-------+-------+-------+
| 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    |
+-----+-------+-------+-------+

My objective is to plot 1 ggplot of multiple boxplots for each key, where each boxplot in the ggplot is the visualization of every column in the dataset. Is there anyway to achieve this?

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

1 个解决方案

#1


2  

As far as I understood

据我所知

 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)

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

or

要么

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

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

#1


2  

As far as I understood

据我所知

 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)

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

or

要么

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

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