1
votes

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?

1
Maybe facet by year? - zx8754
Have you tried scale_x_date? Also post sample data per this guide stackoverflow.com/questions/5963269/… - Tung
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
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
This has been asked here. - markus

1 Answers

0
votes

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()