[英]Force time series x labels on each year
Below I create a reproducible example chart ranging for 10000 days.下面我创建了一个可重现的示例图表,范围为 10000 天。 As you can see this chart is highly informative and value adding, but it will do for the example.
如您所见,此图表信息量很大且增值,但它可以作为示例。
Instead of one x label every 10 years, I would like to force a label every year.而不是每 10 年一个 x label,我想每年强制一个 label。 How can this be achieved?
如何做到这一点?
library(ggplot2)
library(tidyr)
exdays <- 1:10000
exdata <- sin(exdays)
exdate <- as_date("2022-01-01")+days(exdays)
exdat <- tibble(exdate, exdata)
p1 <- ggplot(exdat, aes(x=exdate, y=exdata)) +
geom_line(color="darkred", size=0.7) +
ggtitle("example")
p1
You maybe want this using scale_x_date
with date_breaks
of 1 year where you specify the date_labels
:您可能希望使用
scale_x_date
和 1 年的date_breaks
来指定date_labels
:
library(ggplot2)
library(tidyr)
exdays <- 1:10000
exdata <- sin(exdays)
exdate <- as_date("2022-01-01")+days(exdays)
exdat <- tibble(exdate, exdata)
p1 <- ggplot(exdat, aes(x=exdate, y=exdata)) +
geom_line(color="darkred", size=0.7) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
ggtitle("example")
p1
Output: Output:
This works这有效
p1 <- ggplot(exdat, aes(x=exdate, y=exdata)) +
geom_line(color="darkred", size=0.7) +
ggtitle("example") + scale_x_date(date_breaks = "1 year")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.