简体   繁体   中英

R: Remove extra lines in geom_polygon ggmap

I have this code which produces this map:

library(ggmap)
library(mapdata)

counties <- map_data("county")
iowa_counties <- subset(counties, region=="iowa")
sq_map2 <- get_map(location = c(-92.55191,42.89219),  maptype = "satellite", source = "google", zoom = 9)
ggmap(sq_map2) + 
scale_y_continuous(limits=c(42.51118, 43.26184), expand=c(0,0)) + 
scale_x_continuous(limits=c(-93.0735, -92.03318), expand=c(0,0)) +
geom_polygon(data = iowa_counties, aes(x=long, y=lat, group=group), fill = NA, color = "white")

在此处输入图片说明

As you can see, there is an extra triangle on the top left. How can I get rid of it?

A very simple way is just to reduce the maximum of the y limit to remove that upper point and prevent the construction of the triangle. Just changing the value in scale_y_continuous from 43.26184 to 43.25184 does the trick:

library(ggmap)
library(mapdata)

counties <- map_data("county")
iowa_counties <- subset(counties, region=="iowa")
sq_map2 <- get_map(location = c(-92.55191,42.89219),  maptype = "satellite", source = "google", zoom = 9)
ggmap(sq_map2) + 
scale_y_continuous(limits=c(42.51118, 43.26184), expand=c(0,0)) + 
scale_x_continuous(limits=c(-93.0735, -92.03318), expand=c(0,0)) +
geom_polygon(data = iowa_counties, aes(x=long, y=lat, group=group), fill = NA, color = "white")

在此处输入图片说明

Of course, you could also just filter out the points above a certain latitude in iowa_counties .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM