我有一個很長的資料集,其中包含兩個主要資訊——收集日期和結果。采集日期格式為.Date(df, format="%Y-%m-%d")。我正在尋找按月分組的這些資料的箱線圖。使用 ggplot2 我可以創建一個散點圖并按月格式化軸,但無法將其更改為箱線圖。我是否需要轉換我的資料以按月對箱線圖進行分組?ggplot2 中 aes(group=...) 函式的語法是什么?
謝謝!
ggplot(Results, aes(x = COLDATE, y = `PB(ug/L)`, group=...))
coord_trans(y = "log10")
geom_boxplot(alpha = 0.5)
xlab("Collection Date")
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y")
theme(axis.text.x=element_text(angle=60, hjust=1))
uj5u.com熱心網友回復:
這是一個例子:
Results = data.frame(COLDATE = as.Date("2020-01-01") 1:1000,
PB = runif(1000, min = 0.3, max = 18)^3)
library(tidyverse); # or `library(ggplot2); library(dplyr)` or use base to add the year beforehand
ggplot(Results %>% mutate(year = lubridate::year(COLDATE)),
aes(x = COLDATE, y = PB,
group = lubridate::floor_date(COLDATE, "month")))
geom_boxplot(alpha = 0.5)
labs(x = "Collection Date", y = "PB(ug/L)")
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y")
scale_y_continuous(trans = scales::log10_trans(),
breaks = 10^(-20:20),
labels = scales::label_number_si())
theme(axis.text.x=element_text(angle=60, hjust=1))
facet_wrap(~year, nrow = 1, scales = "free_x")
或者:
ggplot(Results %>%
mutate(year = lubridate::year(COLDATE),
month = lubridate::month(COLDATE, label = TRUE)),
aes(x = as.factor(year), y = PB,
group = year))
geom_boxplot(alpha = 0.5)
labs(x = "Collection Date", y = "PB(ug/L)")
scale_y_continuous(trans = scales::log10_trans(),
breaks = 10^(-20:20),
labels = scales::label_number_si())
theme(axis.text.x=element_text(angle=60, hjust=1))
facet_wrap(~month, nrow = 1, scales = "free_x")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493100.html