ggplot2:删除并替换轴

ggplot2: remove and replace axis

我正在尝试按儒略日绘制时间序列数据集,但 x 轴标签以月为单位(信息量更大的间隔)。在基本绘图函数中,我知道我会使用 xaxt="n"axis 但我无法弄清楚 ggplot 等价物。

示例:

temp <- c(8,10,9,12,15,16,22,12,5,4)
julian_day <- c(1,25,63,65,70,77,150,260,300,350)
temp_month <- c(1, 1, 3, 3, 4, 4, 6, 9, 10, 12)
x <- data.frame(temp, julian_day, temp_month)

ggplot(x, aes(julian_day, temp)) + geom_line() +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

我可以通过正确的变量绘图并删除 x 轴,但我不知道如何添加 x$temp_month

的轴

我尝试添加 scale_x_discrete(breaks=c(1:12), labels=c(1:12)) and/or theme(axis.text.x = buoy_agg$month) 但都不起作用。

我认为这只是找到正确命令的问题。有什么建议吗?

谢谢!

绘制日期时,我强烈建议使用 Date class 数据并使用 scale_x_date - 您可以使用 scale_x_date 的参数轻松自定义轴以显示月份名称、月份缩写、月份编号或任何其他日期格式。

既然你有儒略日,我已将其转换为任意非闰年的日期。只要您不标注年份,我们就可以轻松获得正确的月份标签。

x$date = as.Date("2018-12-31") + x$julian_day

ggplot(x, aes(date, temp)) + 
  geom_line() +
  scale_x_date(date_labels = "%b", name = "")

如果您想要月份数字而不是月份缩写,请将 %b 切换为 %m。有关更多选项和示例,请参阅 ?scale_x_date