我正在使用lapply和update创建一系列颜色,以包含在一系列ggplot弹出窗口中。如演示的here所示,地块将出现在leaflet地图中。什么是“需要一个带有调用组件的对象”错误,我该如何解决它?
x<-structure(list(FacilName = c("ALL ABOU", "ANITA BR", "RAINBOW ", "RANEY DA", "CAS DONO", "A HOME A", "COURT TI", "ECONOMY "),
lon = c(-79.8466921, -79.9265183, -80.240089, -79.7807676, -79.8687159, -79.8064845, -79.8527703, -80.2034522),
lat = c(40.5808574, 40.2919276, 40.6674307, 40.5880096, 40.1815023, 40.3850284, 40.2577138, 40.6867816),
HC01_VC04 = c(4370L, 7700L, 1056L, 584L, 2318L, 1029L, 5053L, 4202L),
HC01_VC87 = c(0L, 0L, 0L, 0L, 31L, 0L, 6L, 22L),
IDnum = c("11", "12", "13", "14", "15", "16", "17", "18")), row.names = 11:18, class = "data.frame")
p<-ggplot(x, aes(HC01_VC87,HC01_VC04))+geom_point() # construct a basic plot
p<-mget(rep("p",length(x$FacilName))) # create a set of plots to pop-up in map with `mget`
clr <- rep("orange", length(x$FacilName)) # default color in scatterplot
# create points that indicates specific facility when popup opens
# error occurs here
p <- lapply(1:length(p), function(i) {
clr[i] <- "dark green"
update(p[[i]], col = clr) })
# map this (will throw error because previous command throws error)
library(leaflet);library(mapview)
leaflet() %>% addTiles()%>%
addCircleMarkers(data=x,label=x$FacilName,
weight = .4,fillOpacity = 1,radius = 4,
popup = popupGraph(p)) 发布于 2019-09-19 23:35:32
我运行了我自己的sessionInfo(),结果如下:

因此,看起来我们确实存在一些版本差异,这可能是问题的一部分。我上周刚在一台新电脑上安装了R。
而且,它看起来不像一个ggplot对象。
下面是我的完整工作代码。我注释掉了你的ggplot并添加了regal库。
library(ggplot2)
library(leaflet)
library(lattice)
library(mapview)
library(sp)
library(rgdal)
x<-structure(list(FacilName = c("ALL ABOU", "ANITA BR", "RAINBOW ", "RANEY DA", "CAS DONO", "A HOME A", "COURT TI", "ECONOMY "),
lon = c(-79.8466921, -79.9265183, -80.240089, -79.7807676, -79.8687159, -79.8064845, -79.8527703, -80.2034522),
lat = c(40.5808574, 40.2919276, 40.6674307, 40.5880096, 40.1815023, 40.3850284, 40.2577138, 40.6867816),
HC01_VC04 = c(4370L, 7700L, 1056L, 584L, 2318L, 1029L, 5053L, 4202L),
HC01_VC87 = c(0L, 0L, 0L, 0L, 31L, 0L, 6L, 22L),
IDnum = c("11", "12", "13", "14", "15", "16", "17", "18")), row.names = 11:18, class = "data.frame")
# commented out ggplot do not run the plot below
# p<-ggplot(x, aes(HC01_VC87,HC01_VC04))+geom_point() # construct a basic plot
# note using lattice and making xyplot
p <- xyplot(HC01_VC04 ~ HC01_VC87, data = x, col = "orange", pch = 20, cex = 2)
p<-mget(rep("p",length(x))) # create a set of plots to pop-up in map with `mget`
clr <- rep("orange", length(x)) # default color in scatterplot
# create points that indicates specific facility when popup opens
# error occurs here
p <- lapply(1:length(p), function(i) {
clr[i] <- "dark green"
update(p[[i]], col = clr) })
# map this (will throw error because previous command throws error)
m <- leaflet() %>%
addTiles()%>%
addCircleMarkers(data=x,label=x$FacilName,
weight = .4,fillOpacity = 1,radius = 4,
popup = popupGraph(p))
m我得到了下面的输出和颜色。

发布于 2019-09-18 05:15:45
您需要确保安装了以下软件包:
library(ggplot2)
library(leaflet)
library(lattice)
library(mapview)
library(sp)在安装库之后,我运行了你所有的代码,并且运行正常。
Mapview需要'sf‘程序包,根据系统的不同,该程序包可能很难安装。

https://stackoverflow.com/questions/57963749
复制相似问题