【发布时间】:2021-11-08 08:28:04
【问题描述】:
我正在尝试根据不同的数据集绘制多个时间序列图。第一个数据集完美绘制。
library(tidyverse)
library(dplyr)
data("economics")
economics %>%
ggplot(aes(date, unemploy, pop)) +
geom_line(col = "maroon") +
xlab("Year") +
ylab("U.S. Unemployment Rate")
第二个数据集可能需要一些额外的条件,但本质上它显示的是相同类型的数据,但绘制的不是相同的。数据可以在这里找到https://fiscaldata.treasury.gov/datasets/debt-to-the-penny/debt-to-the-penny。
library(tidyverse)
library(dplyr)
data(debt)
debt <- read.csv("C:##path here to the data")
debt %>%
filter(Calendar.Month.Number==12 & Calendar.Day.Number==31) %>%
ggplot(aes(Calendar.Year, Debt.Held.by.the.Public)) +
geom_line(col = "blue")
我应该做些什么不同的事情?
【问题讨论】:
-
看起来
debt中的 x 和 y 变量作为因子而不是数字被读入 R。使用str(debt)查看您有哪些列类型。 -
从您提供的 URL 中查看 CSV,我认为最简单的解决方案是使用
read_csv(来自readr,tidyverse 的一部分)而不是read.csv。前者读取具有正确列类型的数据以进行绘图(日期和数字)。 -
谢谢。您知道将 chr 转换为日期的方法吗?这似乎是两者之间的区别。
-
天才。那行得通。
-
我在下面的答案中提供了更多详细信息。如果它有助于解决您的问题,请投票/接受它。
标签: r ggplot2 timeserieschart