0

There seems to be a lot of posts where you want to change the date in ggplot for a specific time-period, and that's easy if you want to just combine some date - however, I want to:

  1. (preferably) set the start value to be the max value of my Date-column (which is in date-format) subtracted by 2 yr or 18/24 months.
  2. if 1 is not feasible, set the start year (and or year and month) and get the x-axis to continue to the latest date in my df.

How do I achieve this?

Thanks!

EDIT: My df:

> str(df_konj)
'data.frame':   1950 obs. of  3 variables:
 $ Indikator: Factor w/ 6 levels "Barometerindikatorn",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Period   : Date, format: "1996-01-01" "1996-02-01" "1996-03-01" "1996-04-01" ...
 $ value    : num  NA NA NA NA NA NA 85.6 88 88.9 91 ...

The "Indikator" col. has 6 series that each is and individual line in the ggplot, since this series is updated through API with new month monthly I need the ggplot to show moving dates - I can not specify the date in "YYYY-MM-DD"-format which all the post I found basically refers to.

The ggplot:

ggplot(df_konj, aes(x = Period, y = value, group=Indikator, color=Indikator, 
                    linetype=Indikator, linewidth = Indikator)) +
  geom_point() + 
  geom_line()

The solution in my head

Some code that says 1. let the end date of x-axis to be the max of the date-col. called "Period" (I think I know that the max value can be extracted by max(Period) - since it's in Date-format 2. and create a variable or do it in the argument that the min x-value is the max-value subtracted by z months - I think this is also possible to do with Date-format objects (but don't remember how). Anyways even if I had the variables I don't know how to pass it into ggplot.

EDIT2 - a solution

This worked, although I'm not sure it's the smartest way of generating the mindate variable - I found a I function on stackoverflow (to subtract months), but it keeps both old and new date, so an additional step is necessary

maxdate <- max(df_konj$Period)
add.months = function(date,n) seq(date, by = paste (n, "months"), length = 2)
mindate <- add.months(maxdate, -18)
mindate <- mindate[2]

Code, which use the logic all(?) post related to specifying exakt dates do:

  scale_x_date(limits=(c(mindate, maxdate))
Christian
  • 117
  • 1
  • 3
  • 10
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a minimal working example of the code you have tried and a snippet of your data or some fake data. – stefan Feb 23 '23 at 10:28
  • Well hopefully this helped someone, I thought this would keep the entire series back to 1996 when I pass this ggplot into plotly - it did not! So I need to find a way of filtering the x-scale in plotly without removing the series-data this is not shown in the 'scaled' output, so you can zoom back to see the history if one is interested in that... – Christian Feb 23 '23 at 11:30

1 Answers1

1

You can set the limits of the x-axis in scale_x_date to the max and min values of your Period variable:

ggplot(df_konj, aes(x = Period, y = value, group = Indikator, color = Indikator, linetype = Indikator, linewidth = Indikator)) +
  geom_point() +
  geom_line() +
  scale_x_date(limits = c(min(df_konj$Period), max(df_konj$Period)))

This only works if Period is in the Date format.

Note:

?scale_x_date for the limits arguments:

A numeric vector of length two providing limits of the scale. Use NA to refer to the existing minimum or maximum.

However, that didn't work for some reason.

Lennart
  • 58
  • 7
  • 1
    Thanks Lennart, I combined the only solution I found on internet with creating the max and min dates outside the ggplot and passing as values **(see EDIT2)**. It worked! :) – Christian Feb 23 '23 at 11:22