繁体   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