我目前正在做一些变量探索,并针对3种不同的气候参数(Tmin,Tmean,Tmax)生成了箱形图。 {3}} 我在线上看过一些教程,但是它们都需要在数据框的行而不是列的开头分配分组参数。我尝试将+ tmax作为参数添加到tmin内,但这产生了错误。我用来生成我的代码如下:
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))
答案 0 :(得分:1)
我使用airquality
数据引用了您的code的原始版本。
想象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)