ggplot World map

I'm trying to create a world map to indicate where documented plant translocations have occurred. What is the error that is causing the problem with my world map image?

My current code-

library(readxl)
library(tidyverse)
library(dplyr)
library(maps)
library(ggplot2)
library(ggthemes)
library("sf")

country_data <- read_excel("Country.xlsx")
View(country_data)

country_data <- country_data %>%
group_by(Country) %>%
summarize(Translocations = n())

View(country_data)

world_map <- map_data("world")

Merge with country data

world_map <- merge(world_map, country_data, by.x = "region", by.y = "Country", all.x = TRUE)

Rename translocations

country_data <- country_data %>%
rename(Translocations_country = Translocations)

Plot graph

gg <- ggplot(world_map)
gg <- gg + geom_map(dat=world_map, map = world_map, aes(map_id=region),
fill="white", color="#7f7f7f", size=0.25)
gg <- gg + geom_map(map = world_map, aes(map_id = region, fill = Translocations), size=0.25)
gg <- gg + scale_fill_gradient(low="lightblue", high="darkblue", name="Number of Translocations")
gg <- gg + expand_limits(x = world_map$long, y = world_map$lat)
gg <- gg + labs(x="", y="", title="Translocations by country")
gg <- gg + theme(panel.grid=element_blank(), panel.border=element_blank())
gg <- gg + theme(axis.ticks=element_blank(), axis.text=element_blank())
gg <- gg + theme(legend.position="top")
gg

Screenshot of data has been uploaded as I wasn't sure the best way to insert this. Please let me now if there is a better way.

Thank you for any help.

Hello! To help us get the data and code, I usually recommend people follow the suggestions here to provide it in an easy format. Also, could you explain more about what "error" you are experiencing? The map looks to be a reasonable representation about what you are trying to model.

Thank you! I will post in this format next time. The problem is that the lines that form the map have random gaps and are incorrect shapes.

Ah yes, I can see what you mean in the filling now when I look closer. I'll play around with some options and circle back to this.

I've noticed something while playing around with this. Your "Country" column does not have matching countries for certain regions in the map data. For example, there is no "England" in the map data but there is a "UK" and there is no "Korea", but there is "South Korea". I suspect this may be causing issues.

Got it! I think merge() may have been causing problems so I switched it for inner_join(). I also switched to geom_polygon() as demonstrated here. There are places you can change color as you'd like. I used the "Country.xlsx" you provided through your picture in your first posting.

library(readxl)
library(tidyverse)
library(dplyr)
library(maps)
library(ggplot2)
library(ggthemes)
library(sf)

country_data <- read_excel("Country.xlsx")

country_data[country_data == "England"] <- "UK"
country_data[country_data == "Korea"] <- "South Korea"

og_world_map <- ggplot2::map_data("world")

world_map <- inner_join(
  og_world_map,
  country_data,
  by = join_by(region == Country)
)

gg <- ggplot(
  data    = world_map,
  mapping = aes(
    x     = long,
    y     = lat,
    group = group
  )
) +
  geom_polygon(
    data  = og_world_map,
    fill  = "white",
    color = "gray"
  ) +
  geom_polygon(
    mapping = aes(fill = Translocations_country)
  ) +
  scale_fill_gradient(
    low  = "lightblue",
    high = "darkblue",
    name = "Number of Translocations"
  ) +
  expand_limits(
    x = world_map$long,
    y = world_map$lat
  ) +
  labs(
    x     = "",
    y     = "",
    title = "Translocations by country"
  ) +
  theme(
    panel.grid   = element_blank(),
    panel.border = element_blank()
  ) +
  theme(
    axis.ticks = element_blank(),
    axis.text  = element_blank()
  )  +
  theme(legend.position = "top")

gg

Thank you so much! This has solved the problem

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.