Reputation: 1844
I am using ggplot to create a map, with a gradient for filling the different regions based on my data.
The default has the map without border lines drawn in. I'd like to include them, and have written some code based on this question. However, this has resulted in too many lines, which connect up all the corners of the regions, I think. How do I avoid this?
In the code below, datafile is where the data I want shown on the map is stored, Scot is the shapefile.
The line that is causing the problem is geom_polygon.
ggplot() +
geom_map(data = datafile, aes(map_id = region, fill = datafile$"2007"), map = Scot) +
geom_polygon(data = Scot, aes(x = Scot$long, y = Scot$lat), colour = "gray", fill = NA) +
expand_limits(x = Scot$long, y=Scot$lat) +
scale_fill_gradient(low = ("lightyellow"), high = ("red"), limits = c(0,35000)) +
ggtitle("2007") +
coord_fixed(1.2) +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.border = element_blank(), panel.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(face = "bold", hjust = 0.5))
Upvotes: 0
Views: 1563
Reputation: 29085
Try adding group = group
to your geom_polygon line. And as Richard Telford remarked, you don't have to use the $ notation inside aes
since you've already indicated the data source via data = Scot
:
... + geom_polygon(data = Scot, aes(x = long, y = lat, group = group),
colour = "gray", fill = NA)
Note: I assumed the Scot data frame was obtained by fortifying a spatial dataset of some sort, which always includes a column named "group". If that's not present, look for the column that indicates which points should belong to the same polygon. The help file for geom_polygon
states (emphasis added):
Polygons are very similar to paths (as drawn by geom_path) except that the start and end points are connected and the inside is coloured by fill. The group aesthetic determines which cases are connected together into a polygon.
Upvotes: 3