英文:
Plotting multiple species on a map using ggplot
问题
I created a map using coordinates of
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)
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...
英文:
I created a map using coordinates of
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)
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.
答案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)
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 <- 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)
<!-- -->
<sup>Created on 2023-06-08 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论