Tôi lấy một bản đồ với hàm dismo::gmap()
và muốn vẽ nó bằng ggplot2 vì tôi muốn thêm các hiệu ứng khác nhau bằng cách sử dụng geom_point và các hàm ggplot khác. Tôi thích sử dụng dismo::gmap
thay vì ggmap::get_map()
để tải xuống lớp bản đồ google. Điều này là do dismo::gmap()
, không giống như ggmap::get_map()
, trả về một lớp raster từ raster gói bao gồm thông tin CRS hoàn chỉnh và do đó sẽ có thể sửa đổi phép chiếu của lớp.google map from dismo :: gmap() và ggplot2
> head(data_info$latitude, 20)
#[1] 49.11306 49.39333 48.78083 51.85000 53.57361 50.67806 52.69083 52.21389 53.46361 50.99917 53.99750 53.54528 53.61417 48.00556 48.01306 53.45000
#[17] 51.93667 54.53083 51.95500 54.29639
> head(data_info$longitude, 20)
#[1] 13.134722 12.323056 13.803889 12.177778 14.143611 13.175833 12.649444 13.454167 11.629722 10.906111 11.415556 8.426944 7.160000 11.123889 10.786111
#[16] 12.766667 11.987222 13.091389 10.967500 13.684167
e = extent(-14 , 58 , 28 , 64)
mapImageData2 <- gmap(e, type = c("terrain"), lonlat = TRUE,
path = "&style=feature:all|element:labels|visibility:off&style=feature:administrative.country|element:geometry.stroke|visibility:off")
mapImageData2_proj <- projectExtent(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84")
# plot the points on the map
ggplot(mapImageData2_proj, extent = "device") +
geom_point(inherit.aes = FALSE, aes(x = data_info$longitude, y = data_info$latitude),
data = gps, colour = "red", size = 1, pch = 20)
Sau khi cố gắng này, tôi nhận được lỗi sau:
Error: ggplot2 doesn't know how to deal with data of class RasterLayer
Nếu tôi cố gắng này
plot(mapImageData2_proj)
Error in .plotraster2(x, col = col, maxpixels = maxpixels, add = add, : no values associated with this RasterLayer
thử 'ggfortify :: fortify (mapImageData2)' to tạo một data.frame mà ggplot sẽ vẽ –