[R] Plotting from different data sources on the same plot (with ggplot2)

hadley wickham h.wickham at gmail.com
Sun Sep 30 18:35:29 CEST 2007


Hi JiHO,

> The ggplot book specifies that "[ggplot] makes it easy to combine
> data from multiple sources". Since I use ggplot2 as much as I can
> (thanks it's really really great!) I thought I would try producing
> such a plot with ggplot2.
>
> NB: If this is possible/easy with an other plotting package please
> let me know. I am not looking for something specific to maps but
> rather for a generic mechanism to throw several pieces of data to a
> graph and have the plotting routine take care of setting up axes that
> will fit all data on the same scale.

I don't think it's easy with any other plotting system (although I'd
be happy to be proven wrong), and was one of the motivations for the
construction of ggplot.

> So, now for the ggplot2 part. I have two data sources: the
> coordinates of the coastlines in a region of interest and the
> coordinated of sampling stations in a subset of this region. I want
> to plot the coastline as a line and the stations as points, on the
> same graph. I can plot them independently easily:
>
> p1 = ggplot(coast,aes(x=lon,y=lat)) + geom_path() + coord_equal(ratio=1)
> p1$aspect.ratio = 1
>
> p2 = ggplot(coords,aes(x=lon,y=lat)) + geom_point() + coord_equal
> (ratio=1)
> p2$aspect.ratio = 1

There are a few ways you could describe the graph you want.  Here's
the one that I'd probably choose:

ggplot(mapping = aes(x = log, y = lat)) +
geom_path(data = coast) +
geom_point(data = coords) +
coord_equal()

We don't define a default dataset in the ggplot call, but instead
explicitly define the dataset in each of the layers. By default,
ggplot will make sure that all the data is displayed on the plot -
i.e. the x and y scales show the union of the ranges over all
datasets.

Does that make sense?

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list