将日期时间数据绘制为月份而不是月年

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

我有一个如下所示的数据框:

structure(list(date = structure(c(1592611200, 1624665600, 1626480000, 
1620086400, 1624147200, 1624752000, 1626566400, 1.566e+09, 1621036800, 
1651536000), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    location = c("POTOMAC RIVER", "POTOMAC RIVER", "POTOMAC RIVER", 
    "POTOMAC RIVER", "POTOMAC RIVER", "POTOMAC RIVER", "POTOMAC RIVER", 
    "POTOMAC RIVER", "POTOMAC RIVER", "POTOMAC RIVER"), avg_temp = c(70.875, 
    83.075, 92.025, 82.6, 87.8, 87.45, 82.15, 89.6, 75.125, 67.85
    )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

当我使用这段代码来绘制它时:

ggplot(df, aes(x=date, y=avg_temp, color = location)) +
  geom_point() +
  ylab("Average Weigh in Time (F)") +
  xlab("Day") 

看起来像这样: enter image description here

我如何编写 ggplot 代码,以便它删除年份并仅按月绘制不同年份的数据,如下所示: enter image description here

r dataframe ggplot2 time-series
1个回答
0
投票

一个选项是将 x 轴切换到 一年中的某一天

library(ggplot2)
ggplot(df, aes(x = lubridate::yday(date), y = avg_temp, color = location)) +
  geom_point() +
  ylab("Average Weigh in Time (F)") +
  xlab("Day of the Year") 

创建于 2024-07-26,使用 reprex v2.1.0

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