I have a dataframe with the latitude and longitudes of lakes I wanted to plot on a map of northeast USA. I'm following this tutorial on how to create maps and plot them.
I can run the code (below) just fine as featured in the tutorial link.
library (ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(rgeos)
world <- ne_countries(scale = "medium", returnclass = "sf")
(sites <- data.frame(longitude = c(-80.144005, -80.109), latitude = c(26.479005, 26.83)))
ggplot(data = world) +
geom_point(data = sites, aes(x = longitude, y = latitude), size = 4,
shape = 23, fill = "darkred") +
coord_sf(xlim = c(-88, -78), ylim = c(24.5, 33), expand = FALSE)
but when I personalize the code for my dataset containing a latitude and longitude column for several lakes in New England, USA, I get an error:
My dataframe of lat and long:
LAT_DD83 LON_DD83
23 41.37213 -71.56798
34 42.33589 -71.90907
39 41.51963 -71.76691
62 41.78447 -71.64064
76 43.93213 -70.62131
129 41.41433 -71.54638
My code:
ggplot(data = world) + geom_sf() +
geom_point(data = lat_lon, aes(x = LON_DD83, y = LAT_DD83), size = 4, shape = 23, fill = "darkred") +
coord_sf(xlim = c(-80, -65), ylim = c(40, 50), expand = FALSE)
ERROR received:
#Error in st_cast.POINT(x[[1]], to, ...) : cannot create MULTILINESTRING from POINT
I don't quite understand what this error means. What am I missing?
So, I'm not sure why, but if you adjust the xlim from 80 to 79 then it works fine. I thought perhaps it could be an issue with projection, but even if the points are converted to an sf
object and matching the projection of world
, then I get the same error with your data. But adjusting the xlim
seems to do the trick:
ggplot(data = world) +
geom_sf() +
geom_point(
data = lat_lon,
aes(x = LON_DD83, y = LAT_DD83),
size = 4,
shape = 23,
fill = "darkred"
) +
coord_sf(xlim = c(-79,-65),
ylim = c(40, 50),
expand = FALSE)
Output
Data
lat_lon <-
structure(list(
LAT_DD83 = c(41.37213, 42.33589, 41.51963, 41.78447,
43.93213, 41.41433),
LON_DD83 = c(
-71.56798,-71.90907,-71.76691,
-71.64064,-70.62131,-71.54638
)
),
class = "data.frame",
row.names = c(NA,-6L))
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.