geom_boxplot 如何将多列合并为一列?
How to merge multiple columns into one for geom_boxplot?
我想使用 ggplot 和 facet_grid 创建多个用于生物标志物分析的箱线图,并按 FILL_FACTOR 对它们进行分层。不幸的是,这些值存储在单独的列中。
我想知道是否有智能解决方案可以为此目的更改数据框。
数据框示例是:
Bio1 Bio2 Bio3 Bio4 FILL_FACTOR
<dbl> <dbl> <dbl> <dbl> <fct>
1 215 160. 1.18 11.0 Survived
2 9 47.2 0.05 13.2 Survived
3 73 6947 5.87 14.3 Died
4 31 2297 6.28 19.3 Died
我们可以这样做。我用 log
代替 y
library(tidyverse)
df %>%
pivot_longer(
-FILL_FACTOR
) %>%
ggplot(aes(x=name, y=log(value), fill=FILL_FACTOR))+
geom_boxplot()
数据:
structure(list(Bio1 = c(215L, 9L, 73L, 31L), Bio2 = c(160, 47.2,
6947, 2297), Bio3 = c(1.18, 0.05, 5.87, 6.28), Bio4 = c(11, 13.2,
14.3, 19.3), FILL_FACTOR = c("Survived", "Survived", "Died",
"Died")), row.names = c("1", "2", "3", "4"), class = "data.frame")
我想使用 ggplot 和 facet_grid 创建多个用于生物标志物分析的箱线图,并按 FILL_FACTOR 对它们进行分层。不幸的是,这些值存储在单独的列中。
我想知道是否有智能解决方案可以为此目的更改数据框。
数据框示例是:
Bio1 Bio2 Bio3 Bio4 FILL_FACTOR
<dbl> <dbl> <dbl> <dbl> <fct>
1 215 160. 1.18 11.0 Survived
2 9 47.2 0.05 13.2 Survived
3 73 6947 5.87 14.3 Died
4 31 2297 6.28 19.3 Died
我们可以这样做。我用 log
代替 y
library(tidyverse)
df %>%
pivot_longer(
-FILL_FACTOR
) %>%
ggplot(aes(x=name, y=log(value), fill=FILL_FACTOR))+
geom_boxplot()
数据:
structure(list(Bio1 = c(215L, 9L, 73L, 31L), Bio2 = c(160, 47.2,
6947, 2297), Bio3 = c(1.18, 0.05, 5.87, 6.28), Bio4 = c(11, 13.2,
14.3, 19.3), FILL_FACTOR = c("Survived", "Survived", "Died",
"Died")), row.names = c("1", "2", "3", "4"), class = "data.frame")