[英]Plot year below month on x axis
1) One easy way is place the year under each January (or use June, say, if you want the year centered -- ie replace 1 with 6 in the code below). 1)一种简单的方法是将年份放在每年的1月份(或者使用6月,例如,如果你想让年份居中 - 即在下面的代码中将1替换为6)。 Another approach is to just replace January with the year by using %Y
in place of %b\\n%Y
. 另一种方法是通过使用%Y
代替%b\\n%Y
来替换1月份。
First introduce some test data -- this should normally be done in the question but I have provided some this time. 首先介绍一些测试数据 - 这通常应该在问题中完成,但这次我提供了一些。 The x
column of the test data frame d
is of class "yearmon"
and y
is numeric. 测试数据帧d
的x
列是"yearmon"
类, y
是数字。 I have assumed that the numbers on the X axis in the question were intended to represent months but if they were intended to be quarters then start off with "yearqtr"
class and use scale_x_yearqtr
instead adjusting appropriately. 我假设问题中X轴上的数字用于表示月份,但如果它们打算用季度,则从"yearqtr"
类开始,然后使用scale_x_yearqtr
相应调整。
library(ggplot2)
library(zoo)
d <- data.frame(x = as.yearmon("2000-1") + 0:23/12, y = 1:24) # test data
x <- d$x
autoplot(d)
ggplot(d, aes(x, y)) +
geom_line() +
geom_point() +
scale_x_yearmon(breaks = x, lab = format(x, ifelse(cycle(x) == 1, "%b\n%Y", "%b")))
2) or if you are starting off with a zoo object z
: 2)或者如果你开始使用动物园对象z
:
z <- read.zoo(d)
x <- time(z)
autoplot(z, geom = c("line", "point")) +
scale_x_yearmon(breaks = x, lab = format(x, ifelse(cycle(x) == 1, "%b\n%Y", "%b"))) +
xlab("")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.