1

I am getting an error using geom_map that I do not get while using geom_polygon when trying to make a choropleth map.

I am following the ggplot2 documentation as closely as possible.

I have a data.frame of positions plus an id column (lease_number) to reference later:

dfmap<- read.table(text="id      long      lat order  hole piece group lease_number
             1 -90.38103 28.78907     1 FALSE     1   1.1        00016
             1 -90.38065 28.82965     2 FALSE     1   1.1        A0016
             1 -90.33457 28.82930     3 FALSE     1   1.1        A0016
             1 -90.33497 28.78872     4 FALSE     1   1.1        A0016
             1 -90.38103 28.78907     5 FALSE     1   1.1        A0016", header=T)

And a data.frame of values with the corresponding id column (just one value here):

df <- data.frame(lease_number="A0016", var1=10)

Following the documentation's structure exactly:

    ggplot(df, aes(fill=var1) +
       geom_map(aes(map_id=lease_number), map=dfmap) +
       expand_limits(dfmap)

Gives the following error:

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

By merging the data like so I can produce a correct plot,

ggplot() +
  geom_polygon(data=merge(dfmap, df, by='lease_number', all=T),
               aes(x=long, y=lat, group=lease_number, fill=var1))

but I want to avoid this because I will need to reference a lot of different things and it will be much better to be able to reference between two data.frames with the lease_number column.

I have seen this question but that answer does not apply here since I cannot even get a base map to show up with geom_map if I remove the fill= argument. Does anyone know how to take on this error?

Thanks.

Community
  • 1
  • 1
moman822
  • 1,904
  • 3
  • 19
  • 33
  • After closely following the second example in the documentation, I changed the variable name in dfmap from "lease_number" to "region" but left it as "lease_number" in df. This seems to give the results you want, but I don't know why. (You'll also need to change how you use `expand_limits` to be like the second example). – aosmith Oct 18 '16 at 21:47

0 Answers0