Home > Enterprise >  R: Change x-axis of a time series analysis with ggplot2
R: Change x-axis of a time series analysis with ggplot2

Time:09-05

I have several years of data and would like to plot the changes during the year. The chart for the whole year from January to December works fine. Now I want to focus only on some months (July to December). How can I change the x-axis? Since the x-axis has the format "date", I can't figure out how it works. I already tried "scale_x_datetime" and "scale_x_continuous" but I always get errors.

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 example

CodePudding user response:

We can create a plot with some made-up data (see below), using your plotting code:

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:

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

To zoom in, we need to add limits to this scale:

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


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")
  • Related