Could not plot geometric point

Hi all,

I am trying to plot a geometric point on Vietnam map. However, I can not do it.
I download the level 1 sp map from GADM. Then I try to coordinate the place with the longitude and latitude. Finally, I try to use geom_point to plot the place but it's not happened. Below is my code

add data information

df = as.data.frame(matrix(nrow = 1, ncol = 3))
colnames(df) = c("lat", "lon", "id")

df$lon = c(21.023397)
df$lat = c(105.806228)
df$id = c(1)

adding plot destination to the map

plot(gadm36_VNM_1_sp) +
geom_point(data = df, aes(x = lon, y = lat), color = "red")

Hi @tung134679,

without having your map data, I notice that you are "mixing" base R plot with ggplot::geom_point - that might be the issue. Also, could you provide the error message you are getting? For plotting maps with ggplot there are many resources among others https://www.r-spatial.org/r/2018/10/25/ggplot2-sf.html

2 Likes

Consider this code:

library(tidyverse)
library(sf)

borders <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_VNM_0_sf.rds"))

points <- data.frame(name = c("here!"),
                     x = c(105.806228),
                     y = c(21.023397)) %>% 
  sf::st_as_sf(coords = c("x","y"), crs=4326)


ggplot() +
  geom_sf(data = borders, fill = NA) +
  geom_sf(data = points, pch = 4, color = "red")

It is built on workflow of {sf} package, and does 3 things:

  • dowloads the Vietnam borders from GADM.org in {sf} format
  • prepares a data frame of coordinates & transforms it to a {sf} format
  • drawing a chart via ggplot; note the use of geom_sf()

4 Likes

Hi @tung134679!

Welcome to the RStudio Community!

Here is an example using ggplot and geom_sf,
with the rnaturalearth package and rnaturalearthdata,
to help you get started.

library(tidyverse)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(rnaturalearth)
library(rnaturalearthdata)
library(rgeos)
#> Loading required package: sp
#> rgeos version: 0.5-2, (SVN revision 621)
#>  GEOS runtime version: 3.7.2-CAPI-1.11.2 
#>  Linking to sp version: 1.3-1 
#>  Polygon checking: TRUE

viet <- ne_countries(country = 'vietnam', type = 'countries', returnclass = 'sf', scale = 'medium')

ggplot(data = viet) + 
  geom_sf() +
  geom_point(aes(x=105, y =22), size =2, shape =23,
             fill = "blue") +
  labs(x = "Longitude East", y = "Latitude North",
       title = "Map of Vietnam", subtitle = "with arbitrary point",
       caption = "data from rnaturalearth mapped with ggplot2 and sf")

Created on 2019-10-18 by the reprex package (v0.3.0)

I found this web post helpful in making maps with sf.

It will help you to get answers to your R questions if you can learn to make a reprex.
A reprex is a reproducible example. This is a nice guide to making your own reprex. This will help you for future questions.

https://www.jessemaegan.com/post/so-you-ve-been-asked-to-make-a-reprex/

Click the heart to like if this reply was helpful.

3 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.