Rachael
Rachael

Reputation: 315

How can I transform my data to create a grouped boxplot in R?

I am a bit stacked trying to create a grouped box plot with ggplot in R. I have a data frame that I need to transform in order to get the desired structure of the data to do it.

My data look as follows:

I have got two data frames:

# 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)

I want to create a grouped box plot comparing F1, F2 and F3 of both methods, for which I have transformed my data frames in order to input the correct values in the function ggplot. I think that the correct structure would be as follows:

enter image description here

For which I have written the following function:

transform_data <- function(df){
  
dlist = list()
  
  for( f in names(df)){
    # The for loop means to create a df for each feature with the desired structure
    
    Values = df$f
    Method = df$method
    data = data.frame(column_value ,  pipeline)
    data$feature = f
    data append to dlist # Can't find the way to do this

  }
  final_df = do.call(rbind, dlist) # The created data frames are finally bound
  return(final_df)
}

After applying the function to both data frames I would rbind both to obtain the final data frame 'data'.

The desired plot would finally be:

ggplot(data, aes(x=Feature, y=Values, fill=Method)) + 
  geom_boxplot()

My dataframes are obviously way more complicated. :(

Any comment will be welcome. Thanks a lot,

Rachel

Upvotes: 0

Views: 324

Answers (1)

Duck
Duck

Reputation: 39613

Maybe you are looking for this. You can use bind_rows() and pivot_longer() keeping the method variable. After that you can design the plot using facets for each method. Here the code:

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())

Output:

enter image description here

Upvotes: 2

Related Questions