gpt4 book ai didi

r - 无法使用常规代码增加 x 轴刻度

转载 作者:行者123 更新时间:2023-12-04 15:07:50 25 4
gpt4 key购买 nike

我有一个我想绘制的每日数据框:


xx=data.frame(Date = seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "day"),
Data = randn(length(seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "day")),1),
stringsAsFactors = F)
然后我像这样绘制它:
ggplot(xx) +
geom_line(aes(x = Date, y = Data), col = "blue") +
labs(y = "", x = "") + theme_classic()

然而,我希望每年都在 x 轴上,当我尝试这样做时:

ggplot(xx) +
geom_line(aes(x = Date, y = Data), col = "blue") +
labs(y = "", x = "") + theme_classic() +
scale_x_continuous(breaks = seq(min(xx$Date), max(xx$Date), by = "year"))

我得到: Error in as.Date.numeric(value) : 'origin' must be supplied .我无法对此进行排序。
谁能帮我?
谢谢!

最佳答案

试试这个:

#Data
xx=data.frame(Date = seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "day"),
Data = randn(length(seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "day")),1),
stringsAsFactors = F)
#Code
ggplot(xx) +
geom_line(aes(x = Date, y = Data), col = "blue") +
labs(y = "", x = "") + theme_classic() +
scale_x_date(breaks = seq(min(xx$Date), max(xx$Date), by = "year"),
date_labels = '%Y')
输出:
enter image description here

关于r - 无法使用常规代码增加 x 轴刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65753163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com