【发布时间】:2019-10-17 23:06:56
【问题描述】:
我有一个由物种名称、经度和纬度坐标组成的数据框。有 115 个不同的物种,具有 25000 纬度/经度坐标。我需要制作单独的地图,显示每个特定物种的观察结果。
首先,我创建了一个函数,它可以生成我想要的那种地图,称为 platmaps。当我为我的完整数据集 (platmaps(df1)) 调用该函数时,它会创建一个显示所有经纬度观测值的地图。
然后我构建了一个 for 循环,它应该按物种名称对我的 df 进行子集化,并将该子集化的数据框插入到我的 platmaps 函数中。它运行了几分钟,然后什么也没有发生。
然后我按物种名称拆分数据框,并创建了一个数据框列表(out1),并使用了lapply(out1, platmaps),但它只返回了我的dfs名称列表。
然后我尝试了我在此处看到的示例的变体,但它也不起作用。
功能
platmaps<-function(df1){
wm <- wm <- borders("world", colour="gray50", fill="gray50")
ggplot()+
coord_fixed()+
wm +
geom_point(data =df1 , aes(x = decimalLongitude, y = decimalLatitude),
colour = "pink", size = 0.5)
子集
for(i in 1:nrow(PP)){
query<-paste(PP$species[i])
p<-subset(df1, df1$species== query))
platmaps(p)
}
列表
for (i in 1:length(out1)){
pp<-out1[[i]]
platmaps(pp)
}
应用示例
p =
wm <- wm <- borders("world", colour="gray50", fill="gray50")
ggplot()+
coord_fixed()+
wm +
geom_point(data =df1 , aes(x = decimalLongitude, y = decimalLatitude),
colour = "pink", size = 0.5)
plots = df1 %>%
group_by(species) %>%
do(plots = p %+% . + facet_wrap(~species))
应用示例的错误是:
错误:无法将 ggproto 对象添加在一起。你忘了添加这个吗 对象到 ggplot 对象?
由于我是 R(和编码)的新手,我假设我的语法错误,或者我的函数没有正确应用到我的任何一个循环中,或者我从根本上误解了循环的工作方式。
数据框示例
species decimalLongitude decimalLatitude
Platanthera lacera -71.90000 42.80000
Platanthera lacera -90.54861 40.12083
Platanthera lacera -71.00889 42.15500
Platanthera lacera -93.20833 45.20028
Platanthera lacera -72.45833 41.91666
Platanthera bifolia 5.19800 59.64310
Platanthera sparsiflora -117.67472 34.36278
固定平台功能
ggplot(data=df1 %>% filter(species == s))+
coord_fixed()+
borders("world", colour="gray50", fill="gray50")+
geom_point(aes(x = decimalLongitude, y = decimalLatitude),
colour = "pink", size = 0.5)+
labs(title=as.character(s))
【问题讨论】:
-
嗨,dplyd,欢迎来到 SO,如果您可以为我们提供一个可重现的示例(如果可能的话,请提供一些数据 see here for an explanation on making a reproducible example)我相信我们可以帮助您解决您的问题。
-
嗨克鲁特,对不起!我添加了一个超链接和我较大的 df 样本的视觉效果。