我對 R 很陌生,我正在嘗試使用 12 個不同的資料框創建一個箱線圖。它們都有一個長度不同的列(熱舒適值),因為它們確實涉及 12 個不同的樣本區域。我試圖創建一個包含不同資料框的串列,因為我知道我不能使用不同大小的不同資料框向量,但是當我嘗試創建箱線圖時遇到此錯誤:sort.int 中的錯誤(x , na.last = na.last, 遞減 = 遞減, ...) : 'x' 必須是原子的
我該如何解決?謝謝,克勞迪婭。
uj5u.com熱心網友回復:
由于您沒有提供任何資料,因此我創建了自己的資料。以下解決方案適用于我:
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()
這個想法是,您向每個資料框添加一列,指示它是哪一個。之后,你rbind
他們(所以不同的長度無關緊要)并使用整個資料框來繪制你的箱線圖。
uj5u.com熱心網友回復:
如果您為 12 個熱舒適值創建單獨的資料框并為它們提供描述性名稱,這可能是使用 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))
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/446307.html
下一篇:在閃亮儀表板標題部分添加字幕