为R中的每一列创建一个箱线图

时间:2018-11-07 20:14:39

标签: r dataframe data-science boxplot

我有一个像这样的数据框:

       X1    X2   X3 ...

Title  One  Two   Three

X1    0     10     19
X2    4     20      3
X3   17     39      3
..     

我想创建一个Boxplots,标题为“ Title”(一个,两个或三个),并在每一列中使用相应的数据。因此,我想为每个列创建一个箱线图。我该怎么办?

Y轴是X1,X2 ...(在最左列)  (应为1、2 ..),x轴为标题。

My data frame

Data frame with columns headers

3 个答案:

答案 0 :(得分:2)

在基数R中:

df <- data.frame(
     'One'   = c( 0,  4, 17), 
     'Two'   = c(10, 20, 39),
     'Three' = c(19,  3,  3))

boxplot(df, main="My Title")

答案 1 :(得分:1)

library(reshape2)
library(ggplot2)

x <- data.frame('One' = c(0, 4, 17), 'Two' = c(10, 20, 39), 'Three' = c(19, 3, 3))
x <- melt(x)

plt <- ggplot(data = x, aes(x = variable, y = value))
plt + geom_boxplot() + theme_minimal() + labs(x = "Title", y = "x")

enter image description here

答案 2 :(得分:0)

df <- data.frame(
 'One'   = c( 0,  4, 17), 
 'Two'   = c(10, 20, 39),
 'Three' = c(19,  3,  3))

lapply(seq_along(df), function(x){
 boxplot(df[[x]], main = paste("Title", colnames(df))[[x]])
})