3

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.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
THUSH2898
  • 33
  • 3
  • I think your question (and the subsequent answers) can help a lot of people who have a similar issue. Is it possible for you to add a (reproducible) code block to your example as described here? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ari Sep 24 '22 at 15:08
  • If we have the exact same data and code you are using then it will be easier for us to debug it and explain the changes that have to be made. – Ari Sep 24 '22 at 15:14
  • 1
    @Ari thank you for your advice! Certainly taking it! Let me update that ASAP. – THUSH2898 Sep 25 '22 at 13:45

3 Answers3

3

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()
Joon
  • 91
  • 2
  • 1
    @Joon. This worked. Thanks! I keep forgetting that '+' is only applicable within the ggplot package. I was trying to use another tutorial, which certainly used this tutorial, but, solution still applies so thanks. Also, thanks for the suggestion. Will be doing that. – THUSH2898 Sep 25 '22 at 13:39
1

As another user pointed out, there is a difference between when you use the + operator and the %>% operator. One mnemonic that may work for you is that the plus sign one "adds layers" to the ggplot, whereas the other operator is connecting code. They really do the same thing in a sense, but because ggplot is literally layering graphics in a plot, that may ensure its easier to remember (and yes, its annoying for first time R users).

I have given an example with map data below given from what looks to be the tutorial you used. First I loaded the requisite libraries after installation:

#### Load Libraries ####
library(tidyverse)
library(maps)
library(mapproj)

Then I saved the world map data into the object world and glimpsed it to see what it is exactly, which appears to be coordinates for a world map.

#### Inspect Data
world <- map_data("world")
glimpse(world)

Rows: 99,338
Columns: 6
$ long      <dbl> -69.89912, -69.89571, -69.94219, -70.00415, -70.0661…
$ lat       <dbl> 12.45200, 12.42300, 12.43853, 12.50049, 12.54697, 12…
$ group     <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2…
$ order     <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 1…
$ region    <chr> "Aruba", "Aruba", "Aruba", "Aruba", "Aruba", "Aruba"…
$ subregion <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …

Then I created the map as a saved object worldmap using the pipes discussed. First, I just used the + operator to make it easy to see that they are only chaining the plot layers on top of each other. There is no need for a pipe here.

#### Create World Map ####
worldmap <- ggplot(world, 
                   aes(x = long, 
                       y = lat,
                       group = group)) +
  geom_path() +
  scale_y_continuous(breaks = (-2:2) * 30) +
  scale_x_continuous(breaks = (-4:4) * 45)
worldmap

enter image description here

Now I will make a very small change to show the difference. The world data was included into the ggplot command last time. This time I'm going to pipe it into the ggplot, which is what the %>% operator does...it simply takes the data and then says "and then use the command after".

worldmap <- world %>% 
  ggplot(aes(x = long, 
           y = lat,
           group = group)) +
  geom_path() +
  scale_y_continuous(breaks = (-2:2) * 30) +
  scale_x_continuous(breaks = (-4:4) * 45)
worldmap

And you get the exact same thing! So just remember the + operator is only for ggplot. Anything else that gets chained together in R uses the %>% pipe (when its possible to do so). For your example, it seems you are also missing the ggplot function and the variables needed to complete it. I have added an example here:

world %>% 
  ggplot(aes(x=long,
             y=lat))+
  coord_map("orthographic", 
            orientation = c(39, -98, 0))

Which gives you this:

enter image description here

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
1

THUSH2898 notes in a revision to their question:

With the help of @Joon, @Hemelstrand who commented with very useful and informative solutions, in addition to @Ari 's advice, I will explain what exactly I was trying to do, by adding [the] final code (solution). I was trying to project the world map using orthographic projection.

The last line of code brought the error. This is what the final code, with the solution should look like, i.e., put the coord_map() line of code within the ggplot block of code, after geom_plot(), for example: This is what I settled with:

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")+
  coord_map("ortho", orientation = c(39, -98, 0))

Plot:Plot

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57