T.Grover
T.Grover

Reputation: 199

ggplot2 plotting coordinates on map using geom_point, unwanted lines appearing between points

I am trying to plot a set of lat/long coordinates on a map of the USA using ggplot2, here is my code:

states <- map_data("state")
usamap <- ggplot(states, aes(long, lat, group=1)) +
  geom_polygon(fill = "white", colour = "black") + 
  geom_point(data = data_masks2, aes(x = lng, y = lat), alpha = 1, size = 1) +
  theme_cowplot()

However, when I plot usamap I am getting strange lines connecting some of the points (seen below), and I am unsure why. Why are these appearing, and how do I get rid of them?

enter image description here

Thanks in advance

Upvotes: 1

Views: 1859

Answers (1)

chemdork123
chemdork123

Reputation: 13883

There's a very helpful vignette available here for creating maps, but the issue is with your geom_polygon() line. You definitely need this (as it's the thing responsible for drawing your state lines), but you have the group= aesthetic wrong. You need to set group=group to correctly draw the lines:

ggplot(states, aes(long, lat, group=group)) +
  geom_polygon(fill = "white", colour = "black")

enter image description here

If you use group=1 as you have, you get the lines:

ggplot(states, aes(long, lat, group=1)) +
  geom_polygon(fill = "white", colour = "black")

enter image description here

Why does this happen? Well, it's how geom_polygon() (and ggplot in general) works. The group= aesthetic tells ggplot what "goes together" for a geom. In the case of geom_polygon(), it tells ggplot what collection of points need to be connected in order to draw a single polygon- which in this case is a single state. When you set group=1, you are assigning every point in the dataset to belong to the same polygon. Believe it or not, the map with the weird lines is actually composed of a single polygon, with points that are drawn in sequence as they are presented.

Have a look at your states dataset and you will see that there is states$group, which is specifically designed to allow you to group the points that belong to each state together. Hence, we arrive at the somewhat confusing statement: group=group. This means "Set the group= aesthetic to the value of the group column in states, or states$group."

Upvotes: 4

Related Questions