在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:

huangapple go评论64阅读模式
英文:

Plotting multiple species on a map using ggplot

问题

I created a map using coordinates of 在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:

I made the map above with this code...

ilwi<-ggplot() +
  geom_polygon(data = w,
               aes(x = long, y = lat, group = group),
               fill = "lightyellow", colour = "black") +
  coord_map() +
  geom_point(data = gent,
             aes(x = long2, y = lat2),
             colour = "royalblue3", size = 2,
             position = "identity") +
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        rect = element_blank())

I am pretty satisfied with how the map looks (would be nice to remove the lat and long labels but). My issue is that my data frame gent has coordinates for two species and the map, from the color of the points, basically does not differentiate between the two species.

Here is a table as an example of my data frame ( a csv with 3 columns)
在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:

Please let me know if you have any suggestions on how to solve this issue! Ideally, the species would be represented by two different colors on the map

So far, I have tried to group acceptedScientificName by the species in aesthetics, I also tried to color and fill by acceptedScientificName as well. Both in quotes and not in quotes. Nothing worked that way I wanted it to.

This is what happened with fill...
在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:

英文:

I created a map using coordinates of 在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:

I made the map above with this code...

ilwi<-ggplot() +
  geom_polygon(data = w,
               aes(x = long, y = lat, group = group),
               fill = "lightyellow", colour = "black") +
  coord_map() +
  geom_point(data = gent,
             aes(x = long2, y = lat2),
             colour = "royalblue3", size = 2,
             position = "identity") +
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        rect = element_blank())

I am pretty satisfied with how the map looks (would be nice to remove the lat and long labels but). My issue is that my data frame gent has coordinates for two species and the map, from the color of the points, basically does not differentiate between the two species.

Here is a table as an example of my data frame ( a csv with 3 columns)
在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:

Please let me know if you have any suggestions on how to solve this issue! Ideally, the species would be represented by two different colors on the map

So far, I have tried to group acceptedScientificName by the species in aesthetics, I also tried to color and fill by acceptedScientificName as well. Both in quotes and not in quotes. Nothing worked that way I wanted it to.

This is what happned with fill...
在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:

答案1

得分: 1

我使用 {usmap} 和一些虚拟的物种数据创建了一个解决方案的草图。物种数据通过 usmap::usmap_transform 函数进行转换,以使点与地图区域对齐。请注意 gent 数据框中变量的顺序,这是由 usmap_transform 的要求所决定的。

最重要的是,物种使用 aes 函数内的填充和颜色进行分组,所使用的形状是可以接受填充和颜色的形状之一,例如形状 21。

library(usmap)
library(ggplot2)


set.seed(123)
# 模拟的物种数据框

gent <- data.frame(lon = -runif(10, 88, 89),
                   lat = runif(10, 40, 45.5),
                   acceptedScientificName = sample(c("sp1", "sp2"), 10, replace = TRUE)
                   )


plot_usmap(include = c("IL", "WI"),
                  fill = "lightyellow") +
  geom_point(data = usmap_transform(gent), 
             aes(x = x, y = y, 
                 fill = acceptedScientificName, 
                 colour = acceptedScientificName),
             shape = 21)

在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:

2023-06-08 创建,使用 reprex v2.0.2

英文:

I've mocked up a solution using {usmap} and some dummy species data. The species data is transformed with the usmap::usmap_transform function so that points line up with the map area. Note the order of variables in the gent data frame which are dictated by the requirements of usmap_transform.

The main thing though, is that species are grouped using fill and colour within the aes function and the shape used is one that can accept fill and colour, for example shape 21.

library(usmap)
library(ggplot2)


set.seed(123)
# mocked up species dataframe

gent &lt;- data.frame(lon = -runif(10, 88, 89),
                   lat = runif(10, 40, 45.5),
                   acceptedScientificName = sample(c(&quot;sp1&quot;, &quot;sp2&quot;), 10, replace = TRUE)
                   )


plot_usmap(include = c(&quot;IL&quot;, &quot;WI&quot;),
                  fill = &quot;lightyellow&quot;) +
  geom_point(data = usmap_transform(gent), 
             aes(x = x, y = y, 
                 fill = acceptedScientificName, 
                 colour = acceptedScientificName),
             shape = 21)

在使用ggplot绘制地图上的多个物种时,您可以使用以下代码:<!-- -->

<sup>Created on 2023-06-08 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月8日 09:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定