Plotting ts in rstudio

Am currently learning R and I would like to plot a graph which looks like this:
image
I have monthly data for data for three years and I would like all the months to be labeled on the x axis. this is what I tried out so far and the error message I received. Thanks for the help
library(ggplot2)

library(lubridate)
setwd("D:/R_scrips")
data1<- read.csv("D:/R_scrips/srf.csv", header=T, stringsAsFactors = FALSE)
data1
Date Rainfall
1 Jan-09 3.1
2 Feb-09 0.2
3 Mar-09 1.2
4 Apr-09 4.1
5 May-09 3.5
6 Jun-09 1.2
7 Jul-09 2.7
8 Aug-09 6.1
9 Sep-09 6.4
10 Oct-09 5.7
11 Nov-09 3.1
12 Dec-09 5.1
13 Jan-10 1.8
14 Feb-10 3.4
15 Mar-10 7.5
16 Apr-10 3.2
17 May-10 7.3
18 Jun-10 6.4
19 Jul-10 6.8
20 Aug-10 3.9
21 Sep-10 3.9
22 Oct-10 6.6
23 Nov-10 1.4
24 Dec-10 0.6
25 Jan-11 0.7
26 Feb-11 0.3
27 Mar-11 3.2
28 Apr-11 3.2
29 May-11 4.9
30 Jun-11 2.8
31 Jul-11 3.8
32 Aug-11 10.0
33 Sep-11 5.6
34 Oct-11 5.6
35 Nov-11 3.9
36 Dec-11 1.3
str(data1)
'data.frame': 36 obs. of 2 variables:
Date : chr "Jan-09" "Feb-09" "Mar-09" "Apr-09" ... Rainfall: num 3.1 0.2 1.2 4.1 3.5 1.2 2.7 6.1 6.4 5.7 ...
data1$Date <- as.Date(as.character(data1$Date), format="d%-%b-%y")
str(data1)
'data.frame': 36 obs. of 2 variables:
Date : Date, format: NA NA NA NA ... Rainfall: num 3.1 0.2 1.2 4.1 3.5 1.2 2.7 6.1 6.4 5.7 ...
ggplot(data1, aes(Time, Rainfall, group = 1)) +

  • geom_line()
    Error in FUN(X[[i]], ...) : object 'Time' not found

ggplot(data1, aes(Date, Rainfall, group = 1)) +

  • geom_line()
    Error in seq.int(0, to0 - from, by) : 'to' must be a finite number

Will this work for you?

library('tidyverse')
library('lubridate')
library('scales')
set.seed(620)
n = 100
d = tibble(Date = dmy(str_c(sample(1:30, n, TRUE), '-',
                            sample(1:12, n, TRUE), '-',
                            sample(1719:1720, n, TRUE))),
           Rainfall = runif(n,0,30))
d %>%
  ggplot(aes(x = Date, y = Rainfall)) +
  geom_line() +
  scale_x_date(labels = date_format("%b"),
               date_breaks = "1 month") +
  theme_minimal()

Hope it helps :slightly_smiling_face:

1 Like

Thanks a lot Leon for your great help. I ran the script and got this output:
image
Two years have been plotted and I still have a challenge of including years below the months. Thanks.

The original plot you posted is not , at least to my knowledge, an out-of-the-can plot. It can be achieved in few minutes, 3-5 I reckon, but that' a matter of aesthetics. @Leon gave you good example that you could adapt., because you are looking for a customised plot.
I'd recommend yo check a bit pars function, but specially, a bit on customising the plots and make us get a Reproducible example , check Andres post FAQ: How to do a minimal reproducible example ( reprex ) for beginners
cheers

Thanks a lot for the hint, let me follow the example

Is this closer to what you need?

df <- data.frame(stringsAsFactors = FALSE,
                 Date = c("Jan-09", "Feb-09", "Mar-09", "Apr-09", "May-09",
                          "Jun-09", "Jul-09", "Aug-09", "Sep-09", "Oct-09",
                          "Nov-09", "Dec-09", "Jan-10", "Feb-10", "Mar-10",
                          "Apr-10", "May-10", "Jun-10", "Jul-10", "Aug-10", "Sep-10",
                          "Oct-10", "Nov-10", "Dec-10", "Jan-11", "Feb-11",
                          "Mar-11", "Apr-11", "May-11", "Jun-11", "Jul-11",
                          "Aug-11", "Sep-11", "Oct-11", "Nov-11", "Dec-11"),
                 Rainfall = c(3.1, 0.2, 1.2, 4.1, 3.5, 1.2, 2.7, 6.1, 6.4, 5.7, 3.1, 5.1,
                              1.8, 3.4, 7.5, 3.2, 7.3, 6.4, 6.8, 3.9, 3.9, 6.6, 1.4, 0.6,
                              0.7, 0.3, 3.2, 3.2, 4.9, 2.8, 3.8, 10, 5.6, 5.6, 3.9, 1.3))
library('tidyverse')
library('lubridate')

df %>%
  mutate(Date = dmy(paste("01-", Date))) %>% 
  ggplot(aes(x = Date, y = Rainfall)) +
  geom_line() +
  scale_x_date(date_labels = "%b-%y",
               date_breaks = "1 month",
               minor_breaks = "1 month",
               expand = c(0,0)) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle=45, hjust=1, vjust = 1))

Created on 2019-04-28 by the reprex package (v0.2.1)

Thank you so much Andres, it is what I need exactly. This is of great help

You can use the sec.axis argument in ggplot2::scale_x_date to show the years opposite the dates:

library(ggplot2)
library(dplyr)

df <- data.frame(stringsAsFactors = FALSE,
                 Date = c("Jan-09", "Feb-09", "Mar-09", "Apr-09", "May-09",
                          "Jun-09", "Jul-09", "Aug-09", "Sep-09", "Oct-09",
                          "Nov-09", "Dec-09", "Jan-10", "Feb-10", "Mar-10",
                          "Apr-10", "May-10", "Jun-10", "Jul-10", "Aug-10", "Sep-10",
                          "Oct-10", "Nov-10", "Dec-10", "Jan-11", "Feb-11",
                          "Mar-11", "Apr-11", "May-11", "Jun-11", "Jul-11",
                          "Aug-11", "Sep-11", "Oct-11", "Nov-11", "Dec-11"),
                 Rainfall = c(3.1, 0.2, 1.2, 4.1, 3.5, 1.2, 2.7, 6.1, 6.4, 5.7, 3.1, 5.1,
                              1.8, 3.4, 7.5, 3.2, 7.3, 6.4, 6.8, 3.9, 3.9, 6.6, 1.4, 0.6,
                              0.7, 0.3, 3.2, 3.2, 4.9, 2.8, 3.8, 10, 5.6, 5.6, 3.9, 1.3))

df <- df %>%
  mutate(
    Year = 2000 + as.numeric(gsub("\\D", "", Date)),
    Month = factor(substr(Date, 1, 3), levels = month.abb, ordered = TRUE)
  ) %>%
  mutate(
    DateVar = as.Date(paste0(Date, " 01"), format = "%b-%y %d")
  ) %>%
  arrange(DateVar)

head(df)
#     Date Rainfall Year Month    DateVar
# 1 Jan-09      3.1 2009   Jan 2009-01-01
# 2 Feb-09      0.2 2009   Feb 2009-02-01
# 3 Mar-09      1.2 2009   Mar 2009-03-01
# 4 Apr-09      4.1 2009   Apr 2009-04-01
# 5 May-09      3.5 2009   May 2009-05-01
# 6 Jun-09      1.2 2009   Jun 2009-06-01

ggplot(df, aes(x = DateVar, y = Rainfall)) +
  geom_line() +
  geom_vline(xintercept = df[["DateVar"]][df[["Month"]] == "Jan"]) +
  scale_x_date(
    breaks = df[["DateVar"]],
    labels = df[["Month"]],
    sec.axis = dup_axis(
      breaks = df[["DateVar"]][df[["Month"]] == "Jul"],
      labels = df[["Year"]][df[["Month"]] == "Jul"]
    )
  )

Of course, it can be made prettier with different colors, line types, and theme settings.

Great thanks for the support. This is a good approach in improving the graph. Thanks a lot!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.