提问人:THUSH2898 提问时间:9/24/2022 最后编辑:Ian CampbellTHUSH2898 更新时间:9/29/2022 访问量:86
是否有运算符可以代替函数 coord_map() 的“+”号?
Is there an operator to use in the place of the '+' sign for the function coord_map()?
问:
我最近开始学习用于空间分析的 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”)
我尝试使用该运算符,但它仍然不起作用。%>%
答:
+ 号是一个特殊运算符,仅用于向 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()
评论
正如另一位用户指出的那样,使用运算符和运算符是有区别的。一个可能适合您的助记符是加号 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
现在,我将做一个非常小的更改来显示差异。上次数据已包含在命令中。这次我要把它输送到 ggplot 中,这就是 %>% 运算符所做的......它只是获取数据,然后说“然后使用命令”。world
ggplot
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))
这给了你这个:
评论
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))
评论