【问题标题】:How to change the a axis to a time series in ggplot2如何在ggplot2中将a轴更改为时间序列
【发布时间】:2021-01-28 15:24:19
【问题描述】:

我正在尝试复制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\Replica of Chicago Fed National Acitivty index (PCA)\dataSet

【问题讨论】:

  • 您的变量time 是数字吗? limits = df$data1.Date 设置轴限制,而不是轴标签。您可以发布示例数据吗?请使用dput(df) 的输出编辑问题。或者,如果 dput(head(df, 20)) 的输出太大。

标签: r ggplot2 time-series stacked-area-chart


【解决方案1】:

不确定您对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"
))

【讨论】:

  • 您好 Stefan,非常感谢您的帮助!我一直坚持这一点,现在一切正常。我希望你度过了愉快的一天:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
相关资源
最近更新 更多