簡體   English   中英

使用 ggplot2 繪制箱線圖時重新排序 x 軸的問題

[英]Issue for reordering x axes when plotting boxplot with ggplot2

我正在繪制一個箱線圖來表示天氣預報 model 中的降雨預報質量。 x 軸是預測時間(天),y 軸是預測結果的整體分布。 藍色方框是后報(過去 20 年的重新預測),紅色方框是預測數據。

# library
library(ggplot2) 
library(readr)
library(forcats)

model_name <- "ecmwf"
hens <- 11
fens <- 51
ys <- 1999
ye <- 2017

# Observation
clim_obs <- as.factor(rep("clim_obs",20))
pcp_obs <- c(80.9737,229.319,111.603,24.0906,53.037,165.04,28.6957,120.151,387.85,155.383,434.328,184.369,169.443,176.654,14.1557,223.796,105.595,56.6908,89.8277,74.0017)
pcp_obs <- as.vector(t(pcp_obs))
obs = data.frame(clim_obs, pcp_obs)

# create a data frame for forecast/hindcast results
lead_time <- factor(rep(seq(1,40),each=hens*(ye-ys+1)+fens),ordered = TRUE,levels = c(seq(1,40)))
Groups <- factor(rep(c("hindcast","forecast"),c(hens*(ye-ys+1),fens)), ordered = TRUE, levels = c("hindcast","forecast"))
pcp <- read_csv(paste(model_name, "_hind_fcst.csv", sep=""))
pcp <- as.vector(t(pcp))
data = data.frame(lead_time, Groups, pcp)
str(data)

# grouped boxplot
p <- ggplot() +  
     varwidth = FALSE) + 
#    geom_boxplot(data=obs, aes(x=clim_obs, y=pcp_obs), alpha = 0.7, outlier.shape = NA, varwidth = FALSE) + 
     geom_boxplot(data=data, aes(x=fct_relevel(lead_time), y=pcp, fill=Groups), alpha = 0.7, outlier.shape = NA, varwidth = FALSE) + 
     labs(x = 'Lead time (day)',
          y = '15-day accumulative rainfall',
          title = '(c) ECMWF') +
     theme_classic() + 
     theme(legend.position = 'bottom', aspect.ratio = 0.35, 
           axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=0.5)) +
     scale_y_continuous(breaks=seq(0,600,50), minor_breaks = seq(0,600,by=10),limits=c(0,600)) + 
     scale_fill_manual(values=c("deepskyblue" , "coral1")) + 
     geom_vline(xintercept = seq(0.5,40.5,by=7), #linetype="dotted", 
                 color = "gray", size=0.25) + 
     ggsave(paste(model_name, "_hind_fcst.pdf", sep="")) 

結果圖在這里: 在此處輸入圖像描述

plot的末尾還有一個白色的方框,是對比觀察數據。 因此,我添加

geom_boxplot(data=obs, aes(x=clim_obs, y=pcp_obs), alpha = 0.7, outlier.shape = NA, varwidth = FALSE) +

但是預測時間的順序是錯誤的。 修改后的圖顯示x軸是字母順序(即1,10,11,12,...,2,21,22,...clim_obs)但我希望它可以是數字順序(即1, 2, 3, 4, 5, ..., clim_obs)

在此處輸入圖像描述

我該如何解決這個問題?

生成數據的文件在這里: link

感謝您花時間在這里!

您可以使用scale_x_discrete來排列 x 軸:

library(ggplot2)

ggplot() +  
  geom_boxplot(data=obs, aes(x=clim_obs, y=pcp_obs), alpha = 0.7, outlier.shape = NA, varwidth = FALSE) + 
  geom_boxplot(data=data, aes(x=lead_time, y=pcp, fill=Groups), alpha = 0.7, outlier.shape = NA, varwidth = FALSE) + 
  labs(x = 'Lead time (day)',
       y = '15-day accumulative rainfall',
       title = '(c) ECMWF') +
  theme_classic() + 
  theme(legend.position = 'bottom', aspect.ratio = 0.35, 
        axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=0.5)) +
  scale_y_continuous(breaks=seq(0,600,50), minor_breaks = seq(0,600,by=10),limits=c(0,600)) + 
  scale_fill_manual(values=c("deepskyblue" , "coral1")) + 
  geom_vline(xintercept = seq(0.5,40.5,by=7), #linetype="dotted", 
             color = "gray", size=0.25) + 
  scale_x_discrete(limits=c(1:40, 'clim_obs'))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM