ggplot2:移除和更换轴

问题描述 投票:0回答:1

我正在尝试通过朱利安·戴(Julian Day)绘制时间序列数据集,但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))和/或theme(axis.text.x = buoy_agg$month),但都没有。

我认为这只是找到正确命令的问题。有提示吗?

谢谢!

r ggplot2 axis
1个回答
2
投票

[绘制日期时,强烈建议您使用Date类数据并使用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 = "")

enter image description here

如果要月号而不是月缩写,则将%b切换为%m。有关更多选项和示例,请参见?scale_x_date

© www.soinside.com 2019 - 2024. All rights reserved.