我有一個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.

uj5u.com熱心網友回復:
嘗試:
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())

uj5u.com熱心網友回復:
更新:
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)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329909.html
上一篇:為什么kotlincoroutines被稱為異步的?
下一篇:如何添加美元符號以懸停?
