使用 R 中 ggplot2 的 facet_wrap 功能对多个数据集进行箱线图比较?
Boxplot comparison of multiple dataset using facet_wrap functionality of ggplot2 in R?
我有一个 data.frame
如下所示。
library(tidyverse)
library(lubridate)
set.seed(129)
DF <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "month"),
O1 = runif(60,1,5), R1 = runif(60,1,5), C1 = runif(60,1,5),
O2 = runif(60,1,5), R2 = runif(60,1,5), C2 = runif(60,1,5),
O3 = runif(60,1,5), R3 = runif(60,1,5), C3 = runif(60,1,5))
目标:
我想制作一个 3 facet
boxplot
,其中 facet
一个是比较 O1, R1, and C1
的数据集。在 facet 2
上,我希望看到 boxplot
的 O2, R2, and C2
,以及第三个 facet
。
示例:
我正在寻找 plot
喜欢的附件。示例图是 2 facet
而我正在寻找 3 facet
.
更新:
DF1 <- DF %>%
as_tibble() %>%
mutate(Date= ymd(Date)) %>%
mutate(month = month(Date),
year = year(Date)) %>%
pivot_longer(
cols = -c(Date, month, year)
) %>%
mutate(facet = case_when(name %in% c("O1", "R1", "C1") ~ "facet1",
name %in% c("O2", "R2", "C2") ~ "facet2",
name %in% c("O3", "R3", "C3") ~ "facet3"))
ggplot(DF1, aes(x=factor(month), y=value)) +
geom_boxplot(aes(fill=name)) +
facet_wrap(.~facet, nrow = 3)
第一个回答:
是这样的吗?
library(tidyverse)
DF %>%
pivot_longer(
cols = -Date
) %>%
mutate(facet = case_when(name %in% c("O1", "R1", "C1") ~ "facet1",
name %in% c("O2", "R2", "C2") ~ "facet2",
name %in% c("O3", "R3", "C3") ~ "facet3")) %>%
ggplot(aes(name, value)) +
geom_boxplot() +
facet_wrap(.~facet, nrow = 3)
尝试:
DF %>%
pivot_longer(-Date) %>%
mutate(month = factor(month.abb[month(Date)], month.abb),
groups = readr::parse_number(name)) %>%
group_by(groups) %>%
mutate(facet_groups = paste0(unique(name), collapse = ","),
name = fct_reorder(name, groups)) %>%
ggplot(aes(x = month, y = value, fill = name)) +
geom_boxplot() +
facet_wrap(facet_groups ~ .,
ncol = 1) +
labs(y = "Monthly Precipitation",
x = element_blank(),
fill = element_blank())
我有一个 data.frame
如下所示。
library(tidyverse)
library(lubridate)
set.seed(129)
DF <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "month"),
O1 = runif(60,1,5), R1 = runif(60,1,5), C1 = runif(60,1,5),
O2 = runif(60,1,5), R2 = runif(60,1,5), C2 = runif(60,1,5),
O3 = runif(60,1,5), R3 = runif(60,1,5), C3 = runif(60,1,5))
目标:
我想制作一个 3 facet
boxplot
,其中 facet
一个是比较 O1, R1, and C1
的数据集。在 facet 2
上,我希望看到 boxplot
的 O2, R2, and C2
,以及第三个 facet
。
示例:
我正在寻找 plot
喜欢的附件。示例图是 2 facet
而我正在寻找 3 facet
.
更新:
DF1 <- DF %>%
as_tibble() %>%
mutate(Date= ymd(Date)) %>%
mutate(month = month(Date),
year = year(Date)) %>%
pivot_longer(
cols = -c(Date, month, year)
) %>%
mutate(facet = case_when(name %in% c("O1", "R1", "C1") ~ "facet1",
name %in% c("O2", "R2", "C2") ~ "facet2",
name %in% c("O3", "R3", "C3") ~ "facet3"))
ggplot(DF1, aes(x=factor(month), y=value)) +
geom_boxplot(aes(fill=name)) +
facet_wrap(.~facet, nrow = 3)
第一个回答: 是这样的吗?
library(tidyverse)
DF %>%
pivot_longer(
cols = -Date
) %>%
mutate(facet = case_when(name %in% c("O1", "R1", "C1") ~ "facet1",
name %in% c("O2", "R2", "C2") ~ "facet2",
name %in% c("O3", "R3", "C3") ~ "facet3")) %>%
ggplot(aes(name, value)) +
geom_boxplot() +
facet_wrap(.~facet, nrow = 3)
尝试:
DF %>%
pivot_longer(-Date) %>%
mutate(month = factor(month.abb[month(Date)], month.abb),
groups = readr::parse_number(name)) %>%
group_by(groups) %>%
mutate(facet_groups = paste0(unique(name), collapse = ","),
name = fct_reorder(name, groups)) %>%
ggplot(aes(x = month, y = value, fill = name)) +
geom_boxplot() +
facet_wrap(facet_groups ~ .,
ncol = 1) +
labs(y = "Monthly Precipitation",
x = element_blank(),
fill = element_blank())