0

I'm trying to do a map to identify specific areas by coloring them. First, I made this plot to check if the data was ok (Setor is the sector's number):

ggplot(aes(x = long, y = lat, fill = Setor), data = mapa2010) + geom_polygon(colour = 'black') # data is ok

enter image description here

Them I tried to made the plot, filling by another variable (AGSN):

ggplot(aes(x = long, y = lat, fill = AGSN), data = mapa2010) + geom_polygon(colour = 'black')

enter image description here

The data is exactly the same, there is no code lines between this 2 commands. I've already tried to reorder the data, but still wrong.

Anyone know why this happens, and how to solve it?

Rcoster
  • 3,170
  • 2
  • 16
  • 35
  • [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – zx8754 Oct 08 '15 at 16:49
  • It's hard to do reproducible example because I don't know why this happens (to recreate in the example), and the original data is big – Rcoster Oct 08 '15 at 16:52
  • Looks like rows in your data frame got re-ordered (mis-ordered). – Gregor Thomas Oct 08 '15 at 17:13
  • @Gregor: I agree with you. But why this happens? I tried sort the data by: `AGSN`, `id`, `piece` and `order`, but didn't changed anything. – Rcoster Oct 08 '15 at 17:59
  • Do you have missing values in `AGSN`? If so you probably got a warning with your plot, but that could do it... – Gregor Thomas Oct 08 '15 at 18:03
  • No missing values. Don't know why, but the second map requires `group = group` in `aes()`. – Rcoster Oct 08 '15 at 18:14

1 Answers1

1

Adding the parameter group = group in aes() for second plot solve. Don't know why only the second map needs.

ggplot(aes(x = long, y = lat, fill = AGSN, group = group), data = mapa2010[order(AGSN, id, piece, order), ]) + geom_polygon(colour = 'black')

enter image description here

Rcoster
  • 3,170
  • 2
  • 16
  • 35