Unknown error using geom_map (choropleth)

662 views
Skip to first unread message

Jake Russ

unread,
Apr 30, 2013, 12:12:28 AM4/30/13
to ggp...@googlegroups.com
Hi all,

I'm having some issues making a choropleth map with geom_map. I don't have a reproducible example, so I realize it may be tough to help me. If I could reproduce the problem, it's likely I could fix it myself, but I'm not even sure where I've gone wrong.

The documentation for geom_map shows this syntax:

ggplot(crimesm, aes(map_id = state)) + geom_map(aes(fill = value), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat) + facet_wrap( ~ variable)

When I run that bit of code the example plots perfect, as expected.

So adapting it to my data frames, I run the following:

m <-ggplot(RegionData1600, aes(map_id = "region"))
m <- m + geom_map(aes(fill = "totals"), map = eurodat_adm1)
m <- m + expand_limits(x = eurodat_adm1$long, y = eurodat_adm1$lat)
(m)

Returning:
Error in unit(x, default.units) : 'x' and 'units' must have length > 0

If I join the two data frames myself, by region, and then plot them using geom_polygon, I get the desired plot. So I'm not quite sure why geom_map is throwing the error. The data.frames are too large for a dput, but I can show you what the first few lines look like below. 

For now I'll just use geom_polygon to get my plots, but I thought it might help to raise this issue on the list also.

Thank you, 

Jake

> head(RegionData1600)
                region quartercentury totals
4561 Baden-Württemberg           1600   1172
4562            Bayern           1600     NA
4563            Berlin           1600     NA
4564       Brandenburg           1600     NA
4565            Bremen           1600     NA
4566           Hamburg           1600     NA

> head(eurodat_adm1)
      long      lat order  hole piece               group      id            region
1 9.650460 49.77634     1 FALSE     1 Baden-Württemberg.1 Germany Baden-Württemberg
2 9.650968 49.76515     2 FALSE     1 Baden-Württemberg.1 Germany Baden-Württemberg
3 9.656839 49.76145     3 FALSE     1 Baden-Württemberg.1 Germany Baden-Württemberg
4 9.640400 49.75014     4 FALSE     1 Baden-Württemberg.1 Germany Baden-Württemberg
5 9.652028 49.74276     5 FALSE     1 Baden-Württemberg.1 Germany Baden-Württemberg
6 9.652208 49.73903     6 FALSE     1 Baden-Württemberg.1 Germany Baden-Württemberg


William Beasley

unread,
Apr 30, 2013, 2:28:04 PM4/30/13
to ggp...@googlegroups.com
If you're going to quote the variables, like "region" below, then use `aes_string` instead of `aes`.

m <-ggplot(RegionData1600, aes(map_id = "region"))

But that doesn't explain why the error isn't thrown after your join the two data frames.  Unless you're not using quoted variables when you define that map.

This is unrelated to your question, but is there a reason why you put your last `m` in parentheses?  Typically that's done only when you're assigning something to the variable, and you want to print the output too.
(m)

Jake Russ

unread,
Apr 30, 2013, 11:52:01 PM4/30/13
to ggp...@googlegroups.com
Wow, it was right in front of me the whole time. Simply taking the quotes off inside aes() fixes the problem with geom_map.

About the m in parentheses, your description of how it's normally used is correct. I have just gotten into the habit of sticking a redundant set of parentheses around the last letter of my ggplots, just as a visual way for me to know that's the end of that plot chunk. Maybe the m by itself should be enough, but sometimes I get myself in the middle of an edit and I lose my place. That's all.

Thank you William! 

Cheers, 

Jake
Reply all
Reply to author
Forward
0 new messages