簡體   English   中英

如何將ggplot2中的a軸改為時間序列

[英]How to change the a axis to a time series in ggplot2

我正在嘗試復制https://www.chicagofed.org/research/data/cfnai/current-data提供的圖表,因為我很快將需要看起來像這樣的數據集圖表。 我快到了,我似乎無法弄清楚如何在使用 ggplot2 時將 x 軸更改為日期。具體來說,我想將其更改為日期列中的日期。 我嘗試了十幾種方法,但沒有任何效果。 該圖的數據在網站的索引下。 這是我的代碼和圖表,其中 dataSet 是來自網站的數據:

library(ggplot2) 
library(reshape2) 
library(tidyverse) 
library(lubridate) 
df = data.frame(time = index(dataSet), melt(as.data.frame(dataSet)))
df
str(df)
df$data1.Date = as.Date(as.character(df$data1.Date))
str(df)
replicaPlot1 = ggplot(df, aes(x = time, y = value)) + 
  geom_area(aes(colour = variable, fill = variable)) +
  stat_summary(fun = sum, geom = "line", size = 0.4) +
  labs(title = "Chicago Fed National Activity Index (CFNAI) Current Data")
replicaPlot1 + scale_x_continuous(name = "time", breaks = waiver(), labels = waiver(), limits = 
df$data1.Date)
replicaPlot1

對此提供任何形式的幫助將不勝感激!

G:\BOS\Common\R-Projects\Graphs\Chicago Fed National Acitivty index (PCA)\dataSet 副本

不確定data.frame(time = index(dataSet), melt(as.data.frame(dataSet)))的意圖是什么。 當我下載數據並通過readxl::read_excel讀取時,我得到了一個帶有日期(時間)列的漂亮小標題,在通過tidyr::pivot_longer重塑后可以很容易地繪制它,並且通過使用scale_x_datetime有一個格式很好的日期軸:

僅使用前 20 行數據試試這個:

library(ggplot2)
library(readxl)
library(tidyr)

df <- pivot_longer(df, -Date, names_to = "variable")

ggplot(df, aes(x = Date, y = value)) +
  geom_area(aes(colour = variable, fill = variable)) +
  stat_summary(fun = sum, geom = "line", size = 0.4) +
  labs(title = "Chicago Fed National Activity Index (CFNAI) Current Data") +
  scale_x_datetime(name = "time")
#> Warning: Removed 4 rows containing non-finite values (stat_summary).
#> Warning: Removed 4 rows containing missing values (position_stack).

reprex package (v1.0.0) 創建於 2021-01-28

數據

# Data downloaded from https://www.chicagofed.org/~/media/publications/cfnai/cfnai-data-series-xlsx.xlsx?la=en

# df <- readxl::read_excel("cfnai-data-series-xlsx.xlsx")

# dput(head(df, 20))
df <- structure(list(Date = structure(c(
  -87004800, -84412800, -81734400,
  -79142400, -76464000, -73785600, -71193600, -68515200, -65923200,
  -63244800, -60566400, -58060800, -55382400, -52790400, -50112000,
  -47520000, -44841600, -42163200, -39571200, -36892800
), tzone = "UTC", class = c(
  "POSIXct",
  "POSIXt"
)), P_I = c(
  -0.26, 0.16, -0.43, -0.09, -0.19, 0.58, -0.05,
  0.21, 0.51, 0.33, -0.1, 0.12, 0.07, 0.04, 0.35, 0.04, -0.1, 0.14,
  0.05, 0.11
), EU_H = c(
  -0.06, -0.09, 0.01, 0.04, 0.1, 0.22, -0.04,
  0, 0.32, 0.16, -0.2, 0.34, 0.06, 0.17, 0.17, 0.07, 0.12, 0.12,
  0.15, 0.18
), C_H = c(
  -0.01, 0.01, -0.05, 0.08, -0.07, -0.01,
  0.12, -0.11, 0.1, 0.15, -0.04, 0.04, 0.17, -0.03, 0.05, 0.08,
  0.09, 0.05, -0.06, 0.09
), SO_I = c(
  -0.01, -0.07, -0.08, 0.02,
  -0.16, 0.22, -0.08, -0.07, 0.38, 0.34, -0.13, -0.1, 0.08, -0.07,
  0.06, 0.07, 0.12, -0.3, 0.35, 0.14
), CFNAI = c(
  -0.34, 0.02, -0.55,
  0.04, -0.32, 1, -0.05, 0.03, 1.32, 0.97, -0.46, 0.39, 0.38, 0.11,
  0.63, 0.25, 0.22, 0.01, 0.49, 0.52
), CFNAI_MA3 = c(
  NA, NA, -0.29,
  -0.17, -0.28, 0.24, 0.21, 0.33, 0.43, 0.77, 0.61, 0.3, 0.1, 0.29,
  0.37, 0.33, 0.37, 0.16, 0.24, 0.34
), DIFFUSION = c(
  NA, NA, -0.17,
  -0.14, -0.21, 0.16, 0.11, 0.17, 0.2, 0.5, 0.41, 0.28, 0.2, 0.32,
  0.36, 0.32, 0.33, 0.25, 0.31, 0.47
)), row.names = c(NA, -20L), class = c(
  "tbl_df",
  "tbl", "data.frame"
))

暫無
暫無

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

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