如何使用 ggplot2 的 facet_wrap 创建时间序列图

How to create a timeseries plot using facet_wrap of ggplot2

这是 上的后续问题。在我之前的问题中,我有一个需要绘制的单一位置数据集,但是,在我目前的情况下,我有多个位置的数据集(总共 11 个)需要绘制在一个图中。我试图通过微小的调整来复制相同的代码,但是,代码没有产生正确的情节。我也没有看到日期在 x 轴上中断。任何帮助将不胜感激。

library(SPEI)
library(tidyverse)
library(zoo)

data("balance")
SPEI_12=spei(balance,12)
SpeiData=SPEI_12$fitted

myDate=as.data.frame(seq(as.Date("1901-01-01"), to=as.Date("2008-12-31"),by="months"))
names(myDate)= "Dates"
myDate$year=as.numeric(format(myDate$Dates, "%Y"))
myDate$month=as.numeric(format(myDate$Dates, "%m"))
myDate=myDate[,-1]
newDates = as.character(paste(month.abb[myDate$month], myDate$year, sep = "_" )) 

DataWithDate = data.frame(newDates,SpeiData) 
df_spei12 = melt(DataWithDate, id.vars = "newDates" )
SPEI12 = df_spei12 %>% 
  na.omit() %>% 
  mutate(sign = ifelse(value >= 0, "pos", "neg")) 
SPEI12 = SPEI12%>% 
  spread(sign,value) %>% 
  replace(is.na(.), 0)

ggplot(SPEI12) + 
  geom_area(aes(x = newDates, y = pos), col = "blue") +
  geom_area(aes(x = newDates, y = neg),  col = "red") +
  facet_wrap(~variable)+
  scale_y_continuous(limits = c(-2.5, 2.5), breaks = -2.5:2.5) +
  scale_x_discrete(breaks=c(1901,1925,1950,1975,2000,2008))+
  ylab("SPEI") + ggtitle("12-Month SPEI") +
  theme_bw() + theme(plot.title = element_text(hjust = 0.5, size = 16, face = "bold"))+
  theme(axis.text = element_text(size=12, colour = "black"), axis.title = element_text(size = 12,face = "bold"))

这是代码生成的内容 - 它生成的不是面积图,而是条形图。

你用了scale_x_discrete()但是你在x-axis、newDates上的变量好像是一个字符。它可以解释为什么 x-axis.

上没有打印任何内容

如果您将 newDates 转换为数字(如您在评论中所建议的那样)

SPEI12$newDates= as.numeric(as.character(gsub(".*_","",SPEI12$newDates)))

并使用 scale_x_continuous() 而不是离散的,你得到这个:

使用 geom_area 我在绘图填充中返回错误(叠加),所以我使用了 geom_bar

library(SPEI)
library(tidyverse)
library(zoo)
library(reshape2)
library(scales)

data("balance")
SPEI_12=spei(balance,12)
SpeiData=SPEI_12$fitted

myDate=as.data.frame(seq(as.Date("1901-01-01"), to=as.Date("2008-12-31"),by="months"))
names(myDate)= "Dates"
myDate$year=as.numeric(format(myDate$Dates, "%Y"))
myDate$month=as.numeric(format(myDate$Dates, "%m"))
myDate=myDate[,-1]
newDates = as.character(paste(month.abb[myDate$month], myDate$year, sep = "_" )) 

DataWithDate = data.frame(newDates,SpeiData) 
df_spei12 = melt(DataWithDate, id.vars = "newDates" )
SPEI12 = df_spei12 %>% 
  na.omit() %>% 
  mutate(sign = ifelse(value >= 0, "pos", "neg")) 
###
SPEI12_md <- SPEI12 %>% 
  dplyr::mutate(Date = lubridate::parse_date_time(newDates, "m_y"),
                Date = lubridate::ymd(Date),
                variable = as.factor(variable))
levels(SPEI12_md$variable) <- c("Indore", "Kimberley", "Albuquerque", "Valencia",
                                "Viena", " Abashiri", "Tampa", "São Paulo", 
                                "Lahore", "Punta Arenas", "Helsinki")

v <- 0.1 # 0.1 it is a gap
v1 <- min(SPEI12_md$value) - v
v2 <- max(SPEI12_md$value) + v

vv <- signif(max(abs(v1), abs(v2)), 2)

ggplot2::ggplot(SPEI12_md) + 
  geom_bar(aes(x = Date, y = value, col = sign, fill = sign),
           show.legend = F, stat = "identity") +
  scale_color_manual(values = c("pos" = "darkblue", "neg" = "red")) +
  scale_fill_manual(values = c("pos"  = "darkblue", "neg" = "red")) +
  facet_wrap(~variable) +
  scale_x_date(date_breaks = "10 years",
               labels = scales::date_format("%Y-%m")) + # 
  scale_y_continuous(limits = c(-vv, vv), breaks = c(seq(-vv-v, 0, length.out = 3),
                                                     seq(0, vv+v,  length.out = 3))) +
  ylab("SPEI") + ggtitle("12-Month SPEI") +
  theme_bw() + theme(plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
                     axis.text = element_text(size=12, colour = "black"),
                     axis.title = element_text(size = 12,face = "bold"),
                     axis.text.x = element_text(angle = 90, size = 10))