用传单绘制世界等值线图
Plotting World chloropeth map with leaflet
我正在尝试绘制 chloropeth 世界地图,但标签显示不正确。我不确定可能是什么问题。请参阅下面的可重现代码。一旦我将光标放在美国上,它就会显示另一个标签。
## Source of shape file
# http://thematicmapping.org/downloads/world_borders.php
## Set working directory ##
## Download the shape files to working directory ##
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="TM_WORLD_BORDERS_SIMPL-0.3.zip")
## Unzip them ##
unzip("TM_WORLD_BORDERS_SIMPL-0.3.zip")
## OR ## You can directly connect to download link and download to a temp folder as well ##
## Load Required Packages##
library(leaflet)
library(rgdal) # R 'Geospatial' Data Abstraction Library. Install package if not already installed.
## Load the shape file to a Spatial Polygon Data Frame (SPDF) using the readOGR() function
myspdf = readOGR(dsn=getwd(), layer="TM_WORLD_BORDERS_SIMPL-0.3")
head(myspdf)
summary(myspdf)
# using the slot data
head(myspdf@data)
head(myspdf$NAME, n=220)
#Get my variable
newdff<-c("Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya", "United States","Canada")
val<-c(1,2,4,5,5,1000,20000, 100)
df<-data.frame(newdff,val)
df
summary(df)
labels <- sprintf(
"<strong>Country:%s</strong><br/>Population:%g",
df$newdff, df$val)%>% lapply(htmltools::HTML)
pal <- colorNumeric("OrRd", df$val)
#
total<-subset(myspdf, myspdf$NAME %in% df$newdff)
head(total@data)
## Create map object and add tiles and polygon layers to it
leaflet(data=total) %>%
addTiles() %>%
addPolygons(fillColor = "green",
highlight = highlightOptions(weight = 1,
color = "red",
fillOpacity = 0.1,
bringToFront = TRUE),
label=labels)
虽然我不完全理解“val”向量的手动创建,
在“标签”创建中引用“总”数据框似乎可行。
## Source of shape file
# http://thematicmapping.org/downloads/world_borders.php
## Set working directory ##
## Download the shape files to working directory ##
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="TM_WORLD_BORDERS_SIMPL-0.3.zip")
## Unzip them ##
unzip("TM_WORLD_BORDERS_SIMPL-0.3.zip")
## OR ## You can directly connect to download link and download to a temp folder as well ##
## Load Required Packages##
library(leaflet)
library(rgdal) # R 'Geospatial' Data Abstraction Library. Install package if not already installed.
## Load the shape file to a Spatial Polygon Data Frame (SPDF) using the readOGR() function
myspdf = readOGR(dsn=getwd(), layer="TM_WORLD_BORDERS_SIMPL-0.3")
head(myspdf)
summary(myspdf)
# using the slot data
head(myspdf@data)
head(myspdf$NAME, n=220)
#Get my variable
newdff<-c("Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya", "United States","Canada")
val<-c(1,2,4,5,5,1000,20000, 100)
df<-data.frame(newdff,val)
df
summary(df)
labels <- sprintf(
"<strong>Country:%s</strong><br/>Population:%g",
df$newdff, df$val)%>% lapply(htmltools::HTML)
pal <- colorNumeric("OrRd", df$val)
#
total<-subset(myspdf, myspdf@data$NAME %in% df$newdff)
labels <- sprintf(
"<strong>Country:%s</strong><br/>Population:%g",
total$NAME, df$val)%>% lapply(htmltools::HTML)
head(total@data)
## Create map object and add tiles and polygon layers to it
leaflet(data=total) %>%
addTiles() %>%
addPolygons(fillColor = "green",
highlight = highlightOptions(weight = 1,
color = "red",
fillOpacity = 0.1,
bringToFront = TRUE),
label=labels)
我正在尝试绘制 chloropeth 世界地图,但标签显示不正确。我不确定可能是什么问题。请参阅下面的可重现代码。一旦我将光标放在美国上,它就会显示另一个标签。
## Source of shape file
# http://thematicmapping.org/downloads/world_borders.php
## Set working directory ##
## Download the shape files to working directory ##
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="TM_WORLD_BORDERS_SIMPL-0.3.zip")
## Unzip them ##
unzip("TM_WORLD_BORDERS_SIMPL-0.3.zip")
## OR ## You can directly connect to download link and download to a temp folder as well ##
## Load Required Packages##
library(leaflet)
library(rgdal) # R 'Geospatial' Data Abstraction Library. Install package if not already installed.
## Load the shape file to a Spatial Polygon Data Frame (SPDF) using the readOGR() function
myspdf = readOGR(dsn=getwd(), layer="TM_WORLD_BORDERS_SIMPL-0.3")
head(myspdf)
summary(myspdf)
# using the slot data
head(myspdf@data)
head(myspdf$NAME, n=220)
#Get my variable
newdff<-c("Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya", "United States","Canada")
val<-c(1,2,4,5,5,1000,20000, 100)
df<-data.frame(newdff,val)
df
summary(df)
labels <- sprintf(
"<strong>Country:%s</strong><br/>Population:%g",
df$newdff, df$val)%>% lapply(htmltools::HTML)
pal <- colorNumeric("OrRd", df$val)
#
total<-subset(myspdf, myspdf$NAME %in% df$newdff)
head(total@data)
## Create map object and add tiles and polygon layers to it
leaflet(data=total) %>%
addTiles() %>%
addPolygons(fillColor = "green",
highlight = highlightOptions(weight = 1,
color = "red",
fillOpacity = 0.1,
bringToFront = TRUE),
label=labels)
虽然我不完全理解“val”向量的手动创建,
在“标签”创建中引用“总”数据框似乎可行。
## Source of shape file
# http://thematicmapping.org/downloads/world_borders.php
## Set working directory ##
## Download the shape files to working directory ##
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="TM_WORLD_BORDERS_SIMPL-0.3.zip")
## Unzip them ##
unzip("TM_WORLD_BORDERS_SIMPL-0.3.zip")
## OR ## You can directly connect to download link and download to a temp folder as well ##
## Load Required Packages##
library(leaflet)
library(rgdal) # R 'Geospatial' Data Abstraction Library. Install package if not already installed.
## Load the shape file to a Spatial Polygon Data Frame (SPDF) using the readOGR() function
myspdf = readOGR(dsn=getwd(), layer="TM_WORLD_BORDERS_SIMPL-0.3")
head(myspdf)
summary(myspdf)
# using the slot data
head(myspdf@data)
head(myspdf$NAME, n=220)
#Get my variable
newdff<-c("Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya", "United States","Canada")
val<-c(1,2,4,5,5,1000,20000, 100)
df<-data.frame(newdff,val)
df
summary(df)
labels <- sprintf(
"<strong>Country:%s</strong><br/>Population:%g",
df$newdff, df$val)%>% lapply(htmltools::HTML)
pal <- colorNumeric("OrRd", df$val)
#
total<-subset(myspdf, myspdf@data$NAME %in% df$newdff)
labels <- sprintf(
"<strong>Country:%s</strong><br/>Population:%g",
total$NAME, df$val)%>% lapply(htmltools::HTML)
head(total@data)
## Create map object and add tiles and polygon layers to it
leaflet(data=total) %>%
addTiles() %>%
addPolygons(fillColor = "green",
highlight = highlightOptions(weight = 1,
color = "red",
fillOpacity = 0.1,
bringToFront = TRUE),
label=labels)