Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

r - How to change the a axis to a time series in ggplot2

I'm trying to replicate the graph provided at https://www.chicagofed.org/research/data/cfnai/current-data since I will be needing graphs for data sets soon that look like this. I'm almost there, I can't seem to figure out how to change the x axis to the dates when using ggplot2. Specifically, I would like to change it to the dates in the Date column. I tried about a dozen ways and nothing is working. The data for this graph is under indexes on the website. Here's my code and the graph where dataSet is the data from the website:

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

Any sort of help on this would be very much appreciated!

G:BOSCommonR-ProjectsGraphsReplica of Chicago Fed National Acitivty index (PCA)dataSet

question from:https://stackoverflow.com/questions/65940115/how-to-change-the-a-axis-to-a-time-series-in-ggplot2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not sure what's your intention with data.frame(time = index(dataSet), melt(as.data.frame(dataSet))). When I download the data and read via readxl::read_excel I got a nice tibble with a date(time) column which after reshaping via tidyr::pivot_longer could easily be plotted and by making use of scale_x_datetime has a nicely formatted date axis:

Using just the first 20 rows of data try this:

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).

Created on 2021-01-28 by the reprex package (v1.0.0)

DATA

# 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"
))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...