我的x轴是时间格式:"2001-01-01,2001-02-01...“如何显示只有年份的轴?我认为scale_x_discrete是我需要使用的函数,但我不知道如何描述中断。
发布于 2020-03-19 12:00:38
您可以使用labels=参数提供一个函数,该函数将以您想要的方式设置日期格式。
scale_x_date(labels = scales::date_format("%Y"))发布于 2020-03-19 12:49:02
除了使用scales包中的format函数之外,还可以使用scale_x_date函数的date_breaks和date_labels参数。
在这里,我将向您展示一个使用lubridate包生成日期序列的示例。您必须确保您的date列实际上是@astrofunkswag所指出的日期格式。
library(lubridate)
df <- data.frame(date = seq(ymd("2001-01-01"), ymd("2005-12-01"), by = "month"),
value = rnorm(60))
str(df)
'data.frame': 60 obs. of 2 variables:
$ date : Date, format: "2001-01-01" "2001-02-01" "2001-03-01" "2001-04-01" ...
$ value: num 0.2839 -0.9031 -0.2851 0.0107 -0.1647 ...
library(ggplot2)
ggplot(df, aes(x = date, y = value))+
geom_line()+
scale_x_date(date_breaks = "year", date_labels = "%Y")

https://stackoverflow.com/questions/60750683
复制相似问题