简体   繁体   English

R:使用 ggplot2 更改时间序列分析的 x 轴

[英]R: Change x-axis of a time series analysis with ggplot2

I have several years of data and would like to plot the changes during the year.我有几年的数据,想 plot 年内的变化。 The chart for the whole year from January to December works fine.从 1 月到 12 月的全年图表运行良好。 Now I want to focus only on some months (July to December).现在我只想关注几个月(7 月到 12 月)。 How can I change the x-axis?如何更改 x 轴? Since the x-axis has the format "date", I can't figure out how it works.由于 x 轴的格式为“日期”,我无法弄清楚它是如何工作的。 I already tried "scale_x_datetime" and "scale_x_continuous" but I always get errors.我已经尝试过“scale_x_datetime”和“scale_x_continuous”,但总是出错。

My data looks like this (example):我的数据如下所示(示例):

values                   Date  
3108013109920            2016-12-25 
3108434522236            2016-08-03
5426634673322            2017-01-07 
6133906789675            2018-11-27 
3679575645687            2018-04-08 
3363868796745            2019-05-23 

And this is my code so far:到目前为止,这是我的代码:

cbPalette <- c("#000000", "#ff9208", "#2adfff", "#00d202", "#ffec16", "#f40300", "#384aff", "#8f0066")

p <- df %>% 
  # make datetime: force unique year
  mutate(datetime = lubridate::make_datetime(2017, month, day)) %>% 
  
  ggplot() +
  geom_line(aes(x = datetime, y = values, color = factor(year)), lwd = 1) +
  scale_x_datetime(breaks = lubridate::make_datetime(2017,1:12), labels = month.abb) +
  scale_colour_manual(values=cbPalette) +

  labs(colour = "Year", x = "Date", y = "Number of water pixels") +
  theme_bw() +
  theme(axis.text=element_text(size=12),
        axis.title=element_text(size=15)) +
  theme(axis.title.x = element_text(vjust=-0.8),
        axis.title.y = element_text(vjust=3),
        axis.text.y = element_text(angle = 90, vjust = 1.5, hjust = 0.5)) +
  theme(legend.title = element_text(size=15),
        legend.text = element_text(size=12))

p

The code creates this plot, and I want to zoom into the red rectangle.代码创建了这个 plot,我想放大红色矩形。

绘图示例

We can create a plot with some made-up data (see below), using your plotting code:我们可以使用您的绘图代码创建带有一些虚构数据(见下文)的 plot:

p <- df %>% 
  mutate(datetime = make_datetime(2017, month, day)) %>% 
  ggplot() +
  geom_line(aes(x = datetime, y = values, color = factor(year)), lwd = 1) +
  scale_colour_manual(values = cbPalette) +
  labs(colour = "Year", x = "Date", y = "Number of water pixels") +
  theme_bw() +
  theme(axis.text    = element_text(size = 12),
        axis.title   = element_text(size = 15),
        axis.title.x = element_text(vjust = -0.8),
        axis.title.y = element_text(vjust =3 ),
        axis.text.y  = element_text(angle = 90, vjust = 1.5, hjust = 0.5),
        legend.title = element_text(size = 15),
        legend.text  = element_text(size  =12))

Using your scale_x_datetime , we can see this is similar to your own plot:使用您的scale_x_datetime ,我们可以看到这类似于您自己的 plot:

p + scale_x_datetime(breaks = make_datetime(2017,1:12), labels = month.abb) 

To zoom in, we need to add limits to this scale:要放大,我们需要为此比例添加limits

p + scale_x_datetime(breaks = make_datetime(2017,1:12), 
                     labels = month.abb, 
                     limits = make_datetime(2017, c(7, 12)))

Created on 2022-09-04 with reprex v2.0.2使用reprex v2.0.2创建于 2022-09-04


Data used使用的数据

library(tidyverse)
library(lubridate)

set.seed(1)
df <- data.frame(date = seq(as.Date("2015-01-01"), by = "15 day", length = 195),
                 values = rpois(195, 2) * runif(195, 10000, 180000)) %>% 
  mutate(day = day(date), year = year(date), month = month(date))

cbPalette <- c("#000000", "#ff9208", "#2adfff", "#00d202", "#ffec16",
               "#f40300", "#384aff", "#8f0066")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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