[R] Customise Plot tick label on time series plot using date series
roslinazairimah zakaria
ro@||n@ump @end|ng |rom gm@||@com
Fri Jan 13 10:34:34 CET 2023
Hi Rui,
Thank you very much. It works beautifully.
On Fri, Jan 13, 2023 at 3:36 PM Rui Barradas <ruipbarradas using sapo.pt> wrote:
> Às 05:11 de 13/01/2023, roslinazairimah zakaria escreveu:
> > Hi,
> >
> > I would like to customise my date series on the plot. I tried this:
> >
> > dt_ts <- ts(dt)
> > autoplot(dt_ts[,2]) + ylab("Charge counts") + xlab("Daily")
> >
> > but the label is not the date series.
> >
> > Tqvm for any help given.
> >
> >
> >> dput(dt)
> > structure(list(time = c("1/1/2014", "2/1/2014", "3/1/2014", "4/1/2014",
> > "5/1/2014", "6/1/2014", "7/1/2014", "8/1/2014", "9/1/2014", "10/1/2014",
> > "11/1/2014", "12/1/2014", "13/1/2014", "14/1/2014", "15/1/2014",
> > "16/1/2014", "17/1/2014", "18/1/2014", "19/1/2014", "20/1/2014",
> > "21/1/2014", "22/1/2014", "23/1/2014", "24/1/2014", "25/1/2014",
> > "26/1/2014", "27/1/2014", "28/1/2014", "29/1/2014", "30/1/2014",
> > "31/1/2014", "1/2/2014", "2/2/2014", "3/2/2014", "4/2/2014",
> > "5/2/2014", "6/2/2014", "7/2/2014", "8/2/2014", "9/2/2014", "10/2/2014",
> > "11/2/2014", "12/2/2014", "13/2/2014", "14/2/2014", "15/2/2014",
> > "16/2/2014", "17/2/2014", "18/2/2014", "19/2/2014", "20/2/2014",
> > "21/2/2014", "22/2/2014", "23/2/2014", "24/2/2014", "25/2/2014",
> > "26/2/2014", "27/2/2014", "28/2/2014", "1/3/2014", "2/3/2014",
> > "3/3/2014", "4/3/2014", "5/3/2014", "6/3/2014", "7/3/2014", "8/3/2014",
> > "9/3/2014", "10/3/2014", "11/3/2014", "12/3/2014", "13/3/2014",
> > "14/3/2014", "15/3/2014", "16/3/2014", "17/3/2014", "18/3/2014",
> > "19/3/2014", "20/3/2014", "21/3/2014", "22/3/2014", "23/3/2014",
> > "24/3/2014", "25/3/2014", "26/3/2014", "27/3/2014", "28/3/2014",
> > "29/3/2014", "30/3/2014", "31/3/2014", "1/4/2014", "2/4/2014",
> > "3/4/2014", "4/4/2014", "5/4/2014", "6/4/2014", "7/4/2014", "8/4/2014",
> > "9/4/2014", "10/4/2014", "11/4/2014", "12/4/2014", "13/4/2014",
> > "14/4/2014", "15/4/2014", "16/4/2014", "17/4/2014", "18/4/2014",
> > "19/4/2014", "20/4/2014", "21/4/2014", "22/4/2014", "23/4/2014",
> > "24/4/2014", "25/4/2014", "26/4/2014", "27/4/2014", "28/4/2014",
> > "29/4/2014", "30/4/2014", "1/5/2014", "2/5/2014", "3/5/2014",
> > "4/5/2014", "5/5/2014", "6/5/2014", "7/5/2014", "8/5/2014", "9/5/2014",
> > "10/5/2014", "11/5/2014", "12/5/2014", "13/5/2014", "14/5/2014",
> > "15/5/2014", "16/5/2014", "17/5/2014", "18/5/2014", "19/5/2014",
> > "20/5/2014", "21/5/2014", "22/5/2014", "23/5/2014", "24/5/2014",
> > "25/5/2014", "26/5/2014", "27/5/2014", "28/5/2014", "29/5/2014",
> > "30/5/2014", "31/5/2014", "1/6/2014", "2/6/2014", "3/6/2014",
> > "4/6/2014", "5/6/2014", "6/6/2014", "7/6/2014", "8/6/2014", "9/6/2014",
> > "10/6/2014", "11/6/2014", "12/6/2014", "13/6/2014", "14/6/2014",
> > "15/6/2014", "16/6/2014", "17/6/2014", "18/6/2014", "19/6/2014",
> > "20/6/2014", "21/6/2014", "22/6/2014", "23/6/2014", "24/6/2014",
> > "25/6/2014", "26/6/2014", "27/6/2014", "28/6/2014", "29/6/2014",
> > "30/6/2014", "1/7/2014", "2/7/2014", "3/7/2014", "4/7/2014",
> > "5/7/2014", "6/7/2014", "7/7/2014", "8/7/2014", "9/7/2014", "10/7/2014",
> > "11/7/2014", "12/7/2014", "13/7/2014", "14/7/2014", "15/7/2014",
> > "16/7/2014", "17/7/2014", "18/7/2014", "19/7/2014", "20/7/2014",
> > "21/7/2014", "22/7/2014", "23/7/2014", "24/7/2014", "25/7/2014",
> > "26/7/2014", "27/7/2014", "28/7/2014", "29/7/2014", "30/7/2014",
> > "31/7/2014", "1/8/2014", "2/8/2014", "3/8/2014", "4/8/2014",
> > "5/8/2014", "6/8/2014", "7/8/2014", "8/8/2014", "9/8/2014", "10/8/2014",
> > "11/8/2014", "12/8/2014", "13/8/2014", "14/8/2014", "15/8/2014",
> > "16/8/2014", "17/8/2014", "18/8/2014", "19/8/2014", "20/8/2014",
> > "21/8/2014", "22/8/2014", "23/8/2014", "24/8/2014", "25/8/2014",
> > "26/8/2014", "27/8/2014", "28/8/2014", "29/8/2014", "30/8/2014",
> > "31/8/2014", "1/9/2014", "2/9/2014", "3/9/2014", "4/9/2014",
> > "5/9/2014", "6/9/2014", "7/9/2014", "8/9/2014", "9/9/2014", "10/9/2014",
> > "11/9/2014", "12/9/2014", "13/9/2014", "14/9/2014", "15/9/2014",
> > "16/9/2014", "17/9/2014", "18/9/2014", "19/9/2014", "20/9/2014",
> > "21/9/2014", "22/9/2014", "23/9/2014", "24/9/2014", "25/9/2014",
> > "26/9/2014", "27/9/2014", "28/9/2014", "29/9/2014", "30/9/2014",
> > "1/10/2014", "2/10/2014", "3/10/2014", "4/10/2014", "5/10/2014",
> > "6/10/2014", "7/10/2014", "8/10/2014", "9/10/2014", "10/10/2014",
> > "11/10/2014", "12/10/2014", "13/10/2014", "14/10/2014", "15/10/2014",
> > "16/10/2014", "17/10/2014", "18/10/2014", "19/10/2014", "20/10/2014",
> > "21/10/2014", "22/10/2014", "23/10/2014", "24/10/2014", "25/10/2014",
> > "26/10/2014", "27/10/2014", "28/10/2014", "29/10/2014", "30/10/2014",
> > "31/10/2014", "1/11/2014", "2/11/2014", "3/11/2014", "4/11/2014",
> > "5/11/2014", "6/11/2014", "7/11/2014", "8/11/2014", "9/11/2014",
> > "10/11/2014", "11/11/2014", "12/11/2014", "13/11/2014", "14/11/2014",
> > "15/11/2014", "16/11/2014", "17/11/2014", "18/11/2014", "19/11/2014",
> > "20/11/2014", "21/11/2014", "22/11/2014", "23/11/2014", "24/11/2014",
> > "25/11/2014", "26/11/2014", "27/11/2014", "28/11/2014", "29/11/2014",
> > "30/11/2014", "1/12/2014", "2/12/2014", "3/12/2014", "4/12/2014",
> > "5/12/2014", "6/12/2014", "7/12/2014", "8/12/2014", "9/12/2014",
> > "10/12/2014", "11/12/2014", "12/12/2014", "13/12/2014", "14/12/2014",
> > "15/12/2014"), cnt_charge_events = c(2L, 3L, 4L, 3L, 5L, 6L,
> > 3L, 5L, 4L, 4L, 6L, 5L, 3L, 3L, 3L, 4L, 7L, 5L, 5L, 5L, 5L, 5L,
> > 4L, 5L, 5L, 6L, 8L, 7L, 4L, 4L, 5L, 4L, 9L, 7L, 7L, 5L, 6L, 10L,
> > 8L, 7L, 10L, 8L, 8L, 10L, 12L, 8L, 10L, 13L, 11L, 9L, 14L, 9L,
> > 11L, 11L, 15L, 15L, 15L, 13L, 17L, 14L, 12L, 20L, 13L, 16L, 16L,
> > 10L, 11L, 12L, 16L, 12L, 15L, 18L, 14L, 9L, 15L, 15L, 16L, 16L,
> > 15L, 15L, 22L, 15L, 16L, 21L, 22L, 19L, 18L, 15L, 17L, 19L, 24L,
> > 23L, 21L, 22L, 15L, 18L, 15L, 17L, 20L, 16L, 13L, 12L, 17L, 14L,
> > 15L, 15L, 19L, 13L, 18L, 11L, 17L, 20L, 23L, 19L, 22L, 20L, 19L,
> > 25L, 21L, 32L, 24L, 20L, 14L, 18L, 17L, 22L, 24L, 24L, 23L, 21L,
> > 21L, 21L, 29L, 25L, 22L, 22L, 17L, 19L, 27L, 28L, 29L, 33L, 28L,
> > 20L, 15L, 16L, 21L, 36L, 28L, 24L, 21L, 21L, 24L, 34L, 29L, 36L,
> > 28L, 23L, 25L, 29L, 28L, 38L, 29L, 23L, 23L, 29L, 25L, 36L, 33L,
> > 26L, 23L, 17L, 20L, 33L, 28L, 33L, 31L, 24L, 17L, 29L, 28L, 32L,
> > 43L, 31L, 27L, 21L, 23L, 26L, 32L, 31L, 34L, 26L, 24L, 24L, 26L,
> > 35L, 34L, 33L, 27L, 23L, 25L, 27L, 31L, 34L, 38L, 24L, 23L, 27L,
> > 40L, 32L, 27L, 34L, 33L, 28L, 23L, 30L, 33L, 28L, 33L, 30L, 23L,
> > 31L, 29L, 34L, 33L, 33L, 27L, 23L, 24L, 38L, 33L, 32L, 39L, 35L,
> > 24L, 28L, 27L, 40L, 42L, 37L, 30L, 21L, 22L, 41L, 36L, 41L, 37L,
> > 36L, 26L, 29L, 35L, 39L, 39L, 48L, 38L, 27L, 34L, 49L, 45L, 52L,
> > 42L, 40L, 27L, 36L, 42L, 51L, 46L, 52L, 39L, 29L, 33L, 39L, 48L,
> > 39L, 50L, 39L, 36L, 37L, 52L, 51L, 50L, 49L, 38L, 33L, 38L, 59L,
> > 55L, 43L, 43L, 37L, 25L, 31L, 32L, 48L, 51L, 49L, 37L, 33L, 23L,
> > 40L, 39L, 49L, 43L, 37L, 30L, 41L, 56L, 54L, 53L, 51L, 46L, 37L,
> > 34L, 56L, 59L, 54L, 50L, 48L, 40L, 39L, 49L, 57L, 51L, 54L, 51L,
> > 37L, 48L, 55L, 64L, 56L, 51L, 47L, 37L, 37L, 55L, 59L, 52L, 57L,
> > 52L, 45L, 42L, 55L, 66L, 59L, 53L, 50L, 40L, 39L, 53L)), class =
> > "data.frame", row.names = c(NA,
> > -349L))
>
> Hello,
>
> There are two main problems with your code:
>
> 1. The time column is not a date, it's a character column. Start by
> coercing it to a real date class.
>
> 2. autoplot is a ggplot2 function and it does not support objects of
> class "ts", you should first coerce the data set dt to class "zoo" or to
> class "xts". In the code below I will coerce to class "zoo".
>
>
> # When calling non-base functions
> # always start the scripts by loading
> # the packages where those functions
> # can be found
> library(ggplot2)
> library(zoo)
>
> # coerce the time column to class "Date"
> dt$time <- as.Date(dt$time, "%d/%m/%Y")
> str(dt)
> # 'data.frame': 349 obs. of 2 variables:
> # $ time : Date, format: "2014-01-01" ...
> # $ cnt_charge_events: int 2 3 4 3 5 6 3 5 4 4 ...
>
>
> # now coerce the time series to a "zoo" time series
> dt_ts <- zoo(dt$cnt_charge_events, order.by = dt$time)
>
> # the x axis labels are right, quarterly date breaks are automatic
> # and plot the object dt_ts, not its second column dt_ts[,2]
> autoplot(dt_ts) +
> xlab("Daily") +
> ylab("Charge counts")
>
>
> # scale_x_date allows for custom breaks and labels,
> # here monthly breaks and the same labels format
> # with more axis labels they need to be rotated in order to be readable
> # (as a side note I have displayed the axis labels in one instruction only)
> autoplot(dt_ts) +
> labs(x = "Daily", y = "Charge counts") +
> scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
> theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1))
>
>
>
> Hope this helps,
>
> Rui Barradas
>
>
--
*Roslinazairimah Zakaria*
*Tel: +609-5492370; Fax. No.+609-5492766*
*Email: roslinazairimah using ump.edu.my <roslinazairimah using ump.edu.my>;
roslinaump using gmail.com <roslinaump using gmail.com>*
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia
[[alternative HTML version deleted]]
More information about the R-help
mailing list