是否有运算符可以代替函数 coord_map() 的“+”号?

Is there an operator to use in the place of the '+' sign for the function coord_map()?

提问人:THUSH2898 提问时间:9/24/2022 最后编辑:Ian CampbellTHUSH2898 更新时间:9/29/2022 访问量:86

问:

我最近开始学习用于空间分析的 R 编程,并尝试运行代码以使用正交投影创建投影地图。问题是,我无法越过“+”号。这是代码和错误。

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))

世界误差 + coord_map(“正交”, orientation = c(39, -98, 0)) : 二进制运算符的非数字参数。
此外:警告消息:“
+”的方法不兼容(“Ops.data.frame”、“+.gg”)

我尝试使用该运算符,但它仍然不起作用。%>%

R 地理空间 数据分析 等值统计

评论

0赞 Ari 9/24/2022
我认为您的问题(以及随后的答案)可以帮助很多有类似问题的人。是否可以在此处所述将(可重现的)代码块添加到示例中?stackoverflow.com/questions/5963269/......
0赞 Ari 9/24/2022
如果我们拥有与您使用的完全相同的数据和代码,那么我们将更容易对其进行调试并解释必须进行的更改。
1赞 THUSH2898 9/25/2022
@Ari感谢您的建议!当然接受它!让我尽快更新。

答:

3赞 Joon 9/24/2022 #1

+ 号是一个特殊运算符,仅用于向 ggplot 对象添加图层。dplyr 的 %>% 运算符用于对象之间的管道。假设 world 是某种数据框对象,您需要先将其通过管道输送到 ggplot 函数中并设置参数。创建此 ggplot 对象后,您可以使用 coord_map 函数将其转换为图形。像这样的东西:

# 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()

评论

1赞 THUSH2898 9/25/2022
@Joon。这奏效了。谢谢!我一直忘记“+”仅适用于 ggplot 包。我正在尝试使用另一个教程,该教程当然使用了本教程,但是,解决方案仍然适用,谢谢。另外,感谢您的建议。会这样做的。
1赞 Shawn Hemelstrand 9/25/2022 #2

正如另一位用户指出的那样,使用运算符和运算符是有区别的。一个可能适合您的助记符是加号 one 向 “添加层”,而另一个运算符是连接代码。从某种意义上说,它们确实在做同样的事情,但由于 ggplot 实际上是在绘图中分层图形,这可能会确保它更容易记住(是的,对于第一次使用 R 的用户来说很烦人)。+%>%ggplot

我给出了一个示例,其中包含下面从您使用的教程中给出的地图数据。首先,我在安装后加载了必要的库:

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

然后,我将世界地图数据保存到对象中,并瞥了一眼它,看看它到底是什么,它似乎是世界地图的坐标。world

#### 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, …

然后,我使用讨论的管道将地图创建为保存的对象。首先,我只是使用运算符来轻松看出它们只是将绘图图层链接在一起。这里不需要管道。worldmap+

#### 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

现在,我将做一个非常小的更改来显示差异。上次数据已包含在命令中。这次我要把它输送到 ggplot 中,这就是 %>% 运算符所做的......它只是获取数据,然后说“然后使用命令”。worldggplot

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

你得到完全一样的东西!因此,请记住,运算符仅用于 .在 R 中链接在一起的任何其他内容都使用管道(如果可能这样做)。对于您的示例,您似乎还缺少完成它所需的函数和变量。我在这里添加了一个示例:+ggplot%>%ggplot

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

这给了你这个:

enter image description here

评论

0赞 THUSH2898 9/25/2022
@Hemelstrand。这也奏效了。谢谢!不能同时投票,但这仍然是一个很好的解决方案。Ps:我通过这段代码学到了很多东西,谢谢!
0赞 Shawn Hemelstrand 9/25/2022
不用担心。很高兴你学到了一些东西。我们都是初学者!
0赞 THUSH2898 9/25/2022
真正。再次感谢。
1赞 Ian Campbell #3

THUSH2898在对他们问题的修订中指出:

在@Joon的帮助下,@Hemelstrand评论了非常有用且内容丰富的解决方案,除了@Ari的建议外,我还将通过添加[最终代码(解决方案)]来解释我到底想做什么。我试图使用正交投影来投影世界地图。

最后一行代码带来了错误。这就是最终代码和解决方案的样子,即将代码行放在 ggplot 代码块中,之后,例如: 这就是我解决的问题:coord_map()geom_plot()

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))

情节:情节