[R] Help with maps
Ray Brownrigg
Ray.Brownrigg at mcs.vuw.ac.nz
Wed Dec 3 02:35:27 CET 2008
On Wed, 03 Dec 2008, Avram Aelony wrote:
> On Tuesday, December 02, 2008, at 04:40PM, "hadley wickham" <h.wickham at gmail.com> wrote:
> >On Tue, Dec 2, 2008 at 6:21 PM, Avram Aelony <aavram at mac.com> wrote:
> >> A few questions about maps...
> >>
> >> (1) How can I find a listing of the internal data sets that map() from
> >> the maps library contains? For example, "usa", "county", "state", "nz"
> >> all work. Are there any others?
> >
> >help(package = maps)
>
> Thanks for this.
>
> >> (2) Is there an easier, more generalized way to produce this
> >> (http://www.ai.rug.nl/~hedderik/R/US2004/ ) type of plot than this
> >> (http://www.ai.rug.nl/~hedderik/R/US2004/map.r ) ? I have geographic
> >> (e.g. country, state, county, zip code) count data in a data frame that
> >> I would like to represent on a map, but still need to study how map.r
> >> works, especially the map.center function...
> >
> >Yes, it's about six lines of ggplot2 code. But a lot depends on the
> >format of your data, so if you could provide a reproducible example of
> >what you're trying to do, that would be very helpful.
>
> What format is best? I don't really have an example, because I only have a
> data frame with geo and count data.
>
> The map() function gives me the following
>
> > d <- data.frame(map("county", plot=FALSE)[c("x","y")])
>
> head> head(d)
> x y
> 1 -86.81457 32.34920 #presumably latitudes and longitudes ???
> 2 -86.81457 32.33774
> 3 -86.80311 32.32628
> 4 -86.79737 32.32055
> 5 -86.78019 32.32628
> 6 -86.78019 32.34347
>
> and I am not sure how to join this with data from a csv formatted file with
> columns for country, state, county, zip code, a, b, c, where a,b,c are
> integers. Ideally, I'd like to show a map of the US by county that
> represents the sum of all "a" in that county with darker colors for larger
> values of x ... Then I'd like to do the same for the UK. If I could do
> the same at the zip code level for certain counties, that would be even
> better.
>
> >> (3) The examples at http://had.co.nz/ggplot2/coord_map.html are great.
> >> Adding another example that with color codes for counts from a data
> >> frame would be very useful too.
> >
> >qplot(..., colour = count) ?
>
> Doesn't that assume that county names and associated counts are mapped to
> lat/lon ? How does the map() function understand "france" or "county" so
> that it can do this internally? I'd like map() to figure out the lat/lon
> details.
>
>
> head> head(d)
> x y
> 1 -86.81457 32.34920
> 2 -86.81457 32.33774
> 3 -86.80311 32.32628
>
>
> Doesn't each row correspond to a vertex? How to get from vertices to
> county names with associated counts?
>
Don't restrict your output to only x and y then. Have a look at:
map("county", plot=FALSE)$names
Here each row corresponds to the polygon (county) represented by the corresponding set of
x and y values in d (up to the next NA).
> >> (4) Is there a reason why I can produce a map of France but not the UK ?
> >>
> >>>library(maps)
> >>>library(ggplot2)
> >>>library(mapproj)
> >>>(qplot(x, y, data=(data.frame(map("france", plot=FALSE)[c("x","y")])),
> >>> geom="path")) + coord_map() (qplot(x, y, data=(data.frame(map("uk",
> >>> plot=FALSE)[c("x","y")])), geom="path")) + coord_map()
> >>
> >> Error in get(dbname) : variable "ukMapEnv" was not found
> >> In addition: Warning message:
> >> In data(list = dbname) : data set 'ukMapEnv' not found
> >
> >map('world', regions="uk")
Again:
map('world', regions="uk")$names
shows you all the islands that are also associated with the UK.
The easiest way would be:
map('world', regions="UK", xlim=c(-10, 5), ylim=c(48, 60))
then use map.axes() to refine this.
HTH
Ray Brownrigg
MSCS, Victoria University of Wellington
>
> This produces a very small UK in the upper left quadrant.
>
> >Hadley
> >
> >--
> >http://had.co.nz/
>
> thanks again in advance
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented, minimal,
> self-contained, reproducible code.
More information about the R-help
mailing list