简体   繁体   English

在x轴上绘制低于月份的年份

[英]Plot year below month on x axis

I'm not looking for help with coding, just a help in what direction I should take, ie what functions to use. 我不是在寻找编码方面的帮助,只是帮助我应该采取什么方向,即使用哪些功能。 I wonder if it is possible to use ggplot to plot something of like of this: 我想知道是否有可能使用ggplot来绘制类似这样的东西:

在此输入图像描述

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. 测试数据帧dx列是"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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM