1

I have the following data frame:

head(DWPhyto)
# A tibble: 6 x 2
        date  data
      <date> <int>
1 2008-10-13   200
2 2009-03-25   200
3 2009-05-03   200
4 2009-05-13   200
5 2009-07-20   200
6 2009-12-22   200

str(DWPhyto)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1364 obs. of  2 variables:
 $ date: Date, format: "2008-10-13" "2009-03-25" ...
 $ data: int  200 200 200 200 200 200 200 200 200 200 ...

When I plot it in ggplot I get a time series of data by time across years 2008-2017 with the following code:

ggplot(DWPhyto, aes(date, data)) +
  geom_line() 

However, I want to split it up into singular years at a time, but when I do by setting xlim() it doesnt change the time series, but it removes the values on the x axis, how do you split it by year? Also once I split it into one year time frames, I want to add labels for each month of the year, but my dataset doesnt have regular sampling dates or sampling frequencies within each month period.

Is this possible?

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    Maybe facet by year? – zx8754 Jan 08 '18 at 07:59
  • Have you tried `scale_x_date`? Also post sample data per this guide https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Tung Jan 08 '18 at 08:01
  • When I use scale_x_date, it makes my labels more descriptive - adds year and month, but doesn't change the time series, nor add labels for months within years. – Brad Skelton Jan 08 '18 at 08:12
  • When I facet wrap by year, I get the following error: Error in combine_vars(data, params$plot_env, vars, drop = params$drop) : At least one layer must contain all variables used for facetting – Brad Skelton Jan 08 '18 at 08:13
  • This has been asked [here](https://stackoverflow.com/questions/48059162/how-to-implement-the-facet-grid-feature-using-the-ggfortify-library-on-a-time-se/48060252#48060252). – markus Jan 08 '18 at 08:26
  • When I follow that guide I get this error:Error: Objects of type tbl_df/tbl/data.frame not supported by autoplot – Brad Skelton Jan 08 '18 at 08:31
  • That's right of course, since you are not dealing with an object of class `ts`. Add `+ facet_grid(. ~ year(date), scales = "free_x") + scale_x_date(date_labels = "%b")` to your plot and make sure the `lubridate` packages is loaded. – markus Jan 08 '18 at 08:34
  • We'd have to see how you are currently setting limits, you can't just say `+ xlim(2007, 2008)` for example, I think you have to give a `Date` object. – Axeman Jan 08 '18 at 09:11
  • you have two questions, one is about splitting the time serie and another about the presetation. If you do not change the data, ggplot will not do it at all, but do you really need it? Regarding the lables, you need to play with `breaks` within the `scale_x_data`. Here an example using pretty_breaks() `scale_x_date( breaks=pretty_breaks(), limits=c(as.Date("1996-01-01"),as.Date("1997-01-01")))` – Marco Jan 08 '18 at 22:55

1 Answers1

0

Here an example to handle the limits and the labels with pretty_breaks():

begining<-as.Date("1995-01-01")
end<- as.Date("1996-01-01")

recordspan  <-c(c(begining, end)) 

ggplot(DWPhyto) +
    geom_line(aes(x=date, y=data), size=0.1) +
    scale_y_continuous(name= "Text of variable y [units]")+
    scale_x_date(breaks=pretty_breaks(),limits=recordspan)+
    theme_bw()
Marco
  • 4,000
  • 3
  • 25
  • 26