使用ggplot在`R`中的多个列的箱线图

时间:2020-06-20 20:37:19

标签: r dataframe ggplot2 boxplot

我只是想在R中创建一个数据框的三个数字列的箱线图。数据框如下所示:

  no_filter                      filter1                   filter2
1 0.7223437                    0.7376562                    0.7418750
2 0.7223437                    0.7376562                    0.7418750
3 0.7262500                    0.7276562                    0.7289062

我在这里How to create one box plot using multiple columns and argument "split"看了一眼,但并没有真正了解它。因此,如果有人有想法,将不胜感激。最好是使用gpplot

1 个答案:

答案 0 :(得分:3)

使用ggplot,我们可能需要重塑为'long'格式

library(dplyr)
library(tidyr)
df1 %>% 
  pivot_longer(cols = everything()) %>% 
  ggplot(aes(x = name, y = value)) +
      geom_boxplot()

enter image description here

### data

df1 <- structure(list(no_filter = c(0.7223437, 0.7223437, 0.72625), 
    filter1 = c(0.7376562, 0.7376562, 0.7276562), filter2 = c(0.741875, 
    0.741875, 0.7289062)), class = "data.frame", row.names = c("1", 
"2", "3"))