Question

my question is very simple, but I have failed to solve it after many attempts. I just want to print some facets of a facetted plot (made with facet_wrap in ggplot2), and remove the ones I am no interested in.

I have facet_wrap with ggplot2, as follows:

#anomalies linear trends
an.trends <- ggplot()+
  geom_smooth(method="lm", data=tndvilong.anomalies, aes(x=year, y=NDVIan, colour=TenureZone,
                                                     group=TenureZone))+
  scale_color_manual(values=miscol) + 
  ggtitle("anomalies' trends")

#anomalies linear trends by VEG
an.trendsVEG <- an.trends + facet_wrap(~VEG,ncol=2)
print(an.trendsVEG)

And I get the plot as I expected (you can see it in te link below):

anomalies' trends by VEG

The question is: how do I get printed only the facest I am interested on? I only want to print "CenKal_ShWoodl", "HlShl_ShDens", "NKal_ShWoodl", and "ThShl_ShDens"

Thanks

Was it helpful?

Solution

I suggest the easiest way to do that is to simply give ggplot() an appropriate subset. In this case:

facets <- c("CenKal_ShWoodl", "HlShl_ShDens", "NKal_ShWoodl", "ThShl_ShDens")
an.trends.sub <- ggplot(tndvilong.anomalies[tndvilong.anomalies$VEG %in% facets,])+
  geom_smooth(method="lm" aes(x=year, y=NDVIan, colour=TenureZone,
                                                     group=TenureZone))+
  scale_color_manual(values=miscol) + 
  ggtitle("anomalies' trends") +
  facet_wrap(~VEG,ncol=2)

Obviously without your data I can't be sure this will give you what you want, but based on your description, it should work. I find that with ggplot, it is generally best to pass it the data you want plotted, rather than finding ways of changing the plot itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top