我有兩個資料框,我想為得分繪制箱線圖,每個資料框箱線圖都有不同的顏色。
ID | 得分1 | 得分 2 |
---|---|---|
1 | 200 | 300 |
2 | 300 | 150 |
3 | 400 | -100 |
ID | 得分1 | 得分 2 |
---|---|---|
200 | 200 | 300 |
300 | 300 | 150 |
400 | 400 | -100 |
uj5u.com熱心網友回復:
這類問題通常與重塑資料有關。格式應為長格式,資料為寬格式。請參閱這篇文章,了解如何將資料從寬格式重塑為長格式。
但在重塑資料之前,創建一個新列來說明該資料來自哪個資料集并系結這兩個資料集。
x <- '
ID score1 score2
1 200 300
2 300 150
3 400 -100'
y <- '
ID score1 score2
200 200 300
300 300 150
400 400 -100'
df1 <- read.table(textConnection(x), header = TRUE)
df2 <- read.table(textConnection(y), header = TRUE)
dfboth <- rbind(
cbind(data = 1, df1),
cbind(data = 2, df2)
)
suppressPackageStartupMessages({
library(dplyr)
library(tidyr)
library(ggplot2)
})
bind_rows(
df1 %>% mutate(data = "1"),
df2 %>% mutate(data = "2")
) %>%
pivot_longer(starts_with("score"), names_to = "score") %>%
ggplot(aes(data, value, fill = score))
geom_boxplot()
xlab("Data set")
由reprex 包(v2.0.1)于 2022-04-29 創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/468205.html