Mapping Points on uk map

Hi I am looking for help to plot points on a map of the UK so far I have the following

The longlat.csv files contains the following
org code, organisation, lat, long, region

I have managed to create the map of the UK but am struggling to put the points on there. Any help greatly appreciated.

library(ggplot2)
library(maps)

worldmap = map_data('world')

knitr::kable(head(worldmap, 20))

ggplot() +
geom_polygon(data = worldmap,
aes(x = long, y = lat, group = group))

ggplot() + geom_polygon(data = worldmap,
aes(x = long,
y = lat,
group = group)) +
coord_fixed(xlim = c(-10,3),
ylim = c(50.3, 59))

ggplot() +
geom_polygon(data = worldmap,
aes(x = long,
y = lat,
group = group)) +
coord_fixed(ratio = 1.3,
xlim = c(-10,3),
ylim = c(50, 59))

library(tidyverse)

ggplot() +
geom_polygon(data = worldmap,
aes(x = long, y = lat,
group = group),
fill = 'gray90',
color = 'black') +
coord_fixed(ratio = 1.3,
xlim = c(-10,3),
ylim = c(50, 59))

ggplot() +
geom_polygon(data = worldmap,
aes(x = long, y = lat,
group = group),
fill = 'gray90',
color = 'black') +
coord_fixed(ratio = 1.3,
xlim = c(-10,3),
ylim = c(50, 59)) +
theme_void()

orgs = read_csv("C:/Users/Digital/Desktop/longlat.csv")
orgs

You have to add orgs's coordinates (lat, long) to plot. Using sf library something like:

points <- st_as_sf(orgs, coords = c("long", "lat"), crs = 4326)

Make sure crs corresponds to your data CRS.

Then add it to plot:

+  geom_sf(data = points)

See Drawing beautiful maps programmatically with R, sf and ggplot2 — Part 2: Layers for examples.

Regards,
Grzegorz

Thanks for your response.

Like so

orgs = read.csv("C:/Users/Digital/Desktop/longlat.csv")
orgs

points <- st_as_sf(orgs, coords = c("long", "lat"), crs = "Data")+
geom_sf(Data = points)

I get the following error message in the console

Error in st_as_sf(orgs, coords = c("long", "lat"), crs = "Data") :
could not find function "st_as_sf"

also should

orgs = read.csv("C:/Users/Digital/Desktop/longlat.csv")
orgs

not be

orgs <- read.csv("C:/Users/Digital/Desktop/longlat.csv")
orgs

It requires sf library, however:

orgs = read_csv("/.../orgs.csv")

ggplot() +
  geom_polygon(
    data = worldmap,
    aes(x = long, y = lat,
        group = group),
    fill = 'gray90',
    color = 'black'
  ) +
  # you can add points to the map
  # as below
  geom_point(data = orgs, 
             mapping = aes(x = long, y = lat)) +

  coord_fixed(ratio = 1.3,
              xlim = c(-10, 3),
              ylim = c(50, 59)) +
  theme_void()

Regards,
Grzegorz

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.