I'm very new to R and i'm trying to create a boxplot with using 12 different data frames. They all have one column (thermal comfort values) with different lenghts because they do refer to 12 different sample areas. I've tried to create a list with the different data frames because i know i can't use different data frames vectors of varying sizes but when i try to create the boxplot a come across with this error: Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must be atomic
How can i fix it? Thanks, Cláudia.
CodePudding user response:
Since you did not provide any data, I created my own. Following solution works on my end:
library(dplyr)
library(ggplot2)
df1 <- data.frame(`Thermal Comfort Values` = c(1, 2, 3, 4, 5, 6, 7, 8, 9))
df2 <- data.frame(`Thermal Comfort Values` = c(10, 11, 12, 14, 13, 15))
lst <- list(df1, df2)
lst <- lapply(1:length(lst), function(x){
lst[[x]] <- lst[[x]] %>%
mutate(name = paste("DF", x))
})
whole <- do.call(rbind, lst)
ggplot(whole, aes(x = name, y = Thermal.Comfort.Values))
geom_boxplot()
The idea is, that you add a column to each dataframe, indicating which one it is. After that, you rbind
them (so different lenghts don't matter) and use the whole dataframe to plot you boxplot.
CodePudding user response:
If you make seperate data frames for the 12 thermal comfort values and give them descriptive names, this could be a quick way to do it using dplyr
a <- data.frame('thermal_comfort_value_A' = c(1,2,3,3,4,2,1))
b <- data.frame('thermal_comfort_value_B' = c(2,3,5,7,8,8,8,5,4,3,2))
c <- data.frame('thermal_comfort_value_C' = c(12,13,35,17,28,28,28,23,2,1,2,1,8,8,0, 0))
boxplot(dplyr::bind_rows(a, b, c))
CodePudding user response:
library(tidyverse)
# Example data - two dataframes of different lengths: 4 and 8 entries
df1 <- data.frame(`Thermal Comfort Value` = runif(4), check.names=FALSE)
df2 <- data.frame(`Thermal Comfort Value` = runif(8), check.names=FALSE)
# Combine them into a list with descriptive names
df_list <- list(`Sampling Area 1`=df1,`Sampling Area 2`=df2)
df_list
# Combine them into one dataframe, with one variable specifying the dataframe names
df_combined <- bind_rows(df_list, .id = "Sampling Area")
df_combined
x_var = "Sampling Area"
y_var = "Thermal Comfort Value"
ggplot(df_combined, mapping = aes(x = .data[[x_var]], y = .data[[y_var]]))
geom_boxplot()
labs(y = y_var, x = x_var)