I recently got into R programming for spatial analysis and was trying to run code to create a projected map using an orthographic projection. Thing, is, I cannot get past the '+' sign. Here's the code and error.
library(tidyverse)
library(maps)
library(mapproj)
world <- map_data("world")
world
world %>%
ggplot()+
geom_map(aes(long, lat, map_id = region),
map = world,
color = "peru", size = 0.5, fill = "seashell")
world +
coord_map("ortho", orientation = c(39, -98, 0))
Error in world + coord_map("orthographic", orientation = c(39, -98, 0)) : non-numeric argument to binary operator.
In addition: Warning message:
Incompatible methods ("Ops.data.frame", "+.gg") for "+"
I have tried using the %>%
operator, but it still won't work.
The + sign is a special operator that is used only for adding layers to ggplot objects. The %>% operator from dplyr is used for piping between objects. Assuming world is some sort of data frame object, you need to pipe it into the ggplot function first and set parameters. After creating this ggplot object, then you can turn it into a graphic using the coord_map function. Something like this:
# Map data
world %>%
# Pipe data into ggplot function, set parameters and shape of map
ggplot(aes(x=add_longitude_here, y=add_latitude_here, group=add_group_here_if_needed)) +
# Add shape of map
geom_polygon(fill = "white", colour = "black") +
# Add correct mercator projection to map
coord_map()