R 中的季节性温度箱线图
Seasonal Temperature Boxplots in R
我目前正在进行一些变量探索,并为 3 个不同的气候参数(Tmin、Tmean、Tmax)生成了箱线图我想知道如何将这些变量分组到一个结构类似于此的箱线图中:
我在网上看过几个教程,但它们都需要在数据框的行而不是列的头部分配分组参数。我尝试在 tmin 中添加 +tmax 作为参数,但出现了错误。我用来生成我的代码如下:
tmin<- ggplot(prism, aes(x = factor(season, levels=c("spring","summer","fall","winter")),
y = tmin_c)) +
geom_boxplot(fill = fill, colour = line, alpha = 0.7) +
theme_bw() +
scale_y_continuous(name = "Temperature C") +
scale_x_discrete(name = "Season") +
ggtitle("MRL Temperature 1980-2013") +
theme(plot.title = element_text(hjust = 0.5))
tmin
已解决,这是最终的工作输出以供将来参考:
#Temperature
dat <- prism
dat <- dat %>%
select(1,2,4,5,6) #1year,2season,4tmin,5tmean,6tmax
dat <- reshape2::melt(dat, measure.vars=3:5)
ggplot(dat, aes(y = value,
x = factor(season, levels=c("spring","summer","fall","winter")),
fill=factor(variable))) +
geom_boxplot() +
theme_bw() +
scale_y_continuous(name = "Temperature C") +
scale_x_discrete(name = "Season") +
ggtitle("MRL Temperature 1980-2013") +
theme(plot.title = element_text(hjust = 0.5))
结果:
我参考了你的 code 使用 airquality
数据的原始版本。
假设 Month
是您的 season
变量。首先,使用 reshape2::melt
将数据重塑为长格式;你的 measure.vars
会是 t_min, t_max, ...
。
dat <- reshape2::melt(airquality, measure.vars=1:3)
summary(dat)
# Temp Month Day variable value
# Min. :56.00 Min. :5.000 Min. : 1.0 Ozone :153 Min. : 1.00
# 1st Qu.:72.00 1st Qu.:6.000 1st Qu.: 8.0 Solar.R:153 1st Qu.: 10.30
# Median :79.00 Median :7.000 Median :16.0 Wind :153 Median : 24.00
# Mean :77.88 Mean :6.993 Mean :15.8 Mean : 80.86
# 3rd Qu.:85.00 3rd Qu.:8.000 3rd Qu.:23.0 3rd Qu.:136.00
# Max. :97.00 Max. :9.000 Max. :31.0 Max. :334.00
# NA's :44
其次,使用 boxplot
和 :
作为 Month
(即您的 season
)因素。
at.key <- c(1:3, 5:7, 9:11, 13:15, 17:19)
# at.key <- (1:(5*4))[(1:(5*4)) %% ((5*4)/4-1) != 0] ## alternatively
b <- boxplot(value ~ variable:Month, border=2:4, col="white",
data=dat, at=at.key, xaxt="n",
main="MRL TMin 1980-2013",
xlab="Season",
ylab="Tmin (C)")
mtext(b$names, 1, .5, at=at.key, cex=.8, las=2)
我目前正在进行一些变量探索,并为 3 个不同的气候参数(Tmin、Tmean、Tmax)生成了箱线图我想知道如何将这些变量分组到一个结构类似于此的箱线图中:
tmin<- ggplot(prism, aes(x = factor(season, levels=c("spring","summer","fall","winter")),
y = tmin_c)) +
geom_boxplot(fill = fill, colour = line, alpha = 0.7) +
theme_bw() +
scale_y_continuous(name = "Temperature C") +
scale_x_discrete(name = "Season") +
ggtitle("MRL Temperature 1980-2013") +
theme(plot.title = element_text(hjust = 0.5))
tmin
已解决,这是最终的工作输出以供将来参考:
#Temperature
dat <- prism
dat <- dat %>%
select(1,2,4,5,6) #1year,2season,4tmin,5tmean,6tmax
dat <- reshape2::melt(dat, measure.vars=3:5)
ggplot(dat, aes(y = value,
x = factor(season, levels=c("spring","summer","fall","winter")),
fill=factor(variable))) +
geom_boxplot() +
theme_bw() +
scale_y_continuous(name = "Temperature C") +
scale_x_discrete(name = "Season") +
ggtitle("MRL Temperature 1980-2013") +
theme(plot.title = element_text(hjust = 0.5))
结果:
我参考了你的 code 使用 airquality
数据的原始版本。
假设 Month
是您的 season
变量。首先,使用 reshape2::melt
将数据重塑为长格式;你的 measure.vars
会是 t_min, t_max, ...
。
dat <- reshape2::melt(airquality, measure.vars=1:3)
summary(dat)
# Temp Month Day variable value
# Min. :56.00 Min. :5.000 Min. : 1.0 Ozone :153 Min. : 1.00
# 1st Qu.:72.00 1st Qu.:6.000 1st Qu.: 8.0 Solar.R:153 1st Qu.: 10.30
# Median :79.00 Median :7.000 Median :16.0 Wind :153 Median : 24.00
# Mean :77.88 Mean :6.993 Mean :15.8 Mean : 80.86
# 3rd Qu.:85.00 3rd Qu.:8.000 3rd Qu.:23.0 3rd Qu.:136.00
# Max. :97.00 Max. :9.000 Max. :31.0 Max. :334.00
# NA's :44
其次,使用 boxplot
和 :
作为 Month
(即您的 season
)因素。
at.key <- c(1:3, 5:7, 9:11, 13:15, 17:19)
# at.key <- (1:(5*4))[(1:(5*4)) %% ((5*4)/4-1) != 0] ## alternatively
b <- boxplot(value ~ variable:Month, border=2:4, col="white",
data=dat, at=at.key, xaxt="n",
main="MRL TMin 1980-2013",
xlab="Season",
ylab="Tmin (C)")
mtext(b$names, 1, .5, at=at.key, cex=.8, las=2)