Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specify different xlim for each plot in facet_wrap (ggplot2)

Tags:

r

ggplot2

that's my code:

ggplot(df, aes(Date, EC))+
  scale_x_date(labels= date_format("%Y"), breaks=date_breaks("year"))+
  geom_point(data=df2, aes(Date, EC), size=2, color="red")+ 
  geom_point()+
  geom_line()+
  xlab(NULL)+
  geom_smooth(method="lm", se=F)+
  facet_wrap(~ID, scales="free_y")+
  theme(axis.text.x = element_text(angle = 90))

That produce this result (I know it's ugly, but take it just as an example): enter image description here

Can I specify different xlim (base on date values) for each plot in facet_wrap? It would be great if I can specify xlim based on the min(df$Date).

I hope I made myself clear.

like image 821
matteo Avatar asked Dec 19 '13 12:12

matteo


1 Answers

This should be solved. Trying on some wide ranged data:

data <- data.frame( grp=rep(c("one","two","three","four"), each=10),
                    x=1:40,
                    y=c( runif(10)*3,
                       ( runif(10)*30 )+10,
                       ( runif(10)*50 )+10,
                       ( runif(10)*10 )-10 ) )

ggplot( data, aes(x, y) ) +
              geom_point(  ) +
              geom_line( ) +
              geom_smooth( method = "loess" ) +
              facet_wrap( ~grp, scales="free" )

ggplot_facet_wrap_scale

like image 157
Andre Wildberg Avatar answered Nov 15 '22 04:11

Andre Wildberg