如何转换数据以在R中创建分组箱线图?

如何转换数据以在R中创建分组箱线图?,r,dataframe,ggplot2,boxplot,R,Dataframe,Ggplot2,Boxplot,我正在尝试用R中的ggplot创建一个分组的方框图。我有一个数据帧需要转换,以便获得所需的数据结构 我的数据如下: 我有两个数据帧: # data frame 1: Method 1 F1 <- c(10,2,3,5,6) F2 <- c(33, 45, 6, 8, 9) F3 <- c(44, 55, 10, 23, 44) Method <- rep("Method1", 5) data1 = data.frame( F1, F2, F3, Me

我正在尝试用R中的ggplot创建一个分组的方框图。我有一个数据帧需要转换,以便获得所需的数据结构

我的数据如下:

我有两个数据帧:

# data frame 1: Method 1

F1 <- c(10,2,3,5,6)
F2 <- c(33, 45, 6, 8, 9)
F3 <- c(44, 55, 10, 23, 44)
Method <- rep("Method1", 5)
data1 = data.frame( F1, F2, F3, Method)

# data frame 2: Method 2

F1 <- c(11,5,3,8,6)
F2 <- c(31, 35, 6, 8, 11)
F3 <- c(44, 55, 12, 23, 41)
Method <- rep("Method2", 5)

data2 = data.frame( F1, F2, F3, Method)
我的数据帧显然要复杂得多(

欢迎发表任何评论。 非常感谢


Rachel

也许您正在寻找这个。您可以使用
bind_rows()
pivot_longer()
保留方法变量。然后,您可以使用每个方法的面设计绘图。代码如下:

library(dplyr)
library(tidyr)
library(ggplot2)
#Code
data1 %>% bind_rows(data2) %>%
  pivot_longer(-Method) %>%
  ggplot(aes(x=name,y=value,fill=name))+
  geom_boxplot()+
  facet_wrap(.~Method,nrow = 1,strip.position = 'bottom')+
  theme_bw()+
  theme(strip.placement = 'outside',
        strip.background = element_blank())
输出:

ggplot(data, aes(x=Feature, y=Values, fill=Method)) + 
  geom_boxplot()
library(dplyr)
library(tidyr)
library(ggplot2)
#Code
data1 %>% bind_rows(data2) %>%
  pivot_longer(-Method) %>%
  ggplot(aes(x=name,y=value,fill=name))+
  geom_boxplot()+
  facet_wrap(.~Method,nrow = 1,strip.position = 'bottom')+
  theme_bw()+
  theme(strip.placement = 'outside',
        strip.background = element_blank())