delaye
delaye

Reputation: 1399

set limits for scale_x_date in ggplot2 in facet_grid context

I have a data frame named "a" as :

             date individus annee
80 2013-07-23         0  2013
77 2013-07-12         0  2013
63 2013-05-13         7  2013
72 2013-06-25         2  2013
7  2011-04-19        20  2011
58 2013-04-23         6  2013
4  2011-04-11         7  2011
52 2012-07-03         0  2012
56 2012-08-06         9  2012
6  2011-04-15         0  2011
38 2012-05-02         8  2012
67 2013-05-28         1  2013
66 2013-05-24         0  2013
59 2013-04-26        46  2013
73 2013-06-28         9  2013
74 2013-07-02         0  2013
22 2011-06-14        44  2011
70 2013-06-17         0  2013
41 2012-05-11         0  2012
14 2011-05-13         6  2011
42 2012-05-15         0  2012
27 2011-07-18         0  2011
18 2011-05-26         0  2011
36 2012-04-13        39  2012
31 2011-07-29        12  2011
55 2012-07-13        25  2012
49 2012-06-14        17  2012
50 2012-06-18        69  2012
51 2012-06-25        65  2012
57 2013-04-19        41  2013

I would like to plot this data with ggplot2, whit facet_grid on annee with this code

plot<-ggplot(data=lob.df)+
#   geom_point(aes(x=date, y=individus))+
  geom_smooth(aes(x=date, y=individus, colour=annee))+
  labs(x="Date",y="Nombre d'individus")+
  scale_colour_discrete(name="Année")+
  facet_grid(.~annee)

how give me that :

ggplot graph with facet_grid

And I would like to remove all blanc data ... so I have played with scale_y_date but I wasn't able to reduce the graph limit :-S

Upvotes: 0

Views: 663

Answers (1)

nrussell
nrussell

Reputation: 18612

You can set the scales parameter in facet_grid to be "free_x" like this:

plot<-ggplot(data=df)+
  #   geom_point(aes(x=date, y=individus))+
  geom_smooth(aes(x=date, y=individus, colour=annee))+
  labs(x="Date",y="Nombre d'individus")+
  facet_grid(.~annee,scales="free_x")
##
print(plot)

enter image description here

I had to modify the aesthetics of your plot a little bit because your code was not running on my machine (I'm not using a very recent release of R), but using facet_grid(.~annee,scales="free_x") should still work fine for you.

Data:

df <- read.table(
  text="             date individus annee
80 2013-07-23         0  2013
77 2013-07-12         0  2013
63 2013-05-13         7  2013
72 2013-06-25         2  2013
7  2011-04-19        20  2011
58 2013-04-23         6  2013
4  2011-04-11         7  2011
52 2012-07-03         0  2012
56 2012-08-06         9  2012
6  2011-04-15         0  2011
38 2012-05-02         8  2012
67 2013-05-28         1  2013
66 2013-05-24         0  2013
59 2013-04-26        46  2013
73 2013-06-28         9  2013
74 2013-07-02         0  2013
22 2011-06-14        44  2011
70 2013-06-17         0  2013
41 2012-05-11         0  2012
14 2011-05-13         6  2011
42 2012-05-15         0  2012
27 2011-07-18         0  2011
18 2011-05-26         0  2011
36 2012-04-13        39  2012
31 2011-07-29        12  2011
55 2012-07-13        25  2012
49 2012-06-14        17  2012
50 2012-06-18        69  2012
51 2012-06-25        65  2012
57 2013-04-19        41  2013")
##
df$date <- as.Date(df$date)
df$individus <- as.numeric(df$individus)
df$annee <- as.numeric(df$annee)

Upvotes: 1

Related Questions