问题描述
问题
我已经加载了UK的shapefile,没有问题.让我们称之为FILE1
.我也有一个包含2列的数据框文件:经度,纬度.我们称之为FILE2
.我正在尝试将FILE2
的坐标绘制到FILE1
的shapefile映射上并创建一个Choropleth映射
I have loaded in the shapefile of the UK, no issues. Lets call this FILE1
.I also have a data frame file with 2 columns: Longitude, Latitude. lets call this FILE2
. I am trying to plot the coordinates of FILE2
onto the shapefile map of FILE1
and create a choropleth map
我也收到以下错误:
代码
我已经从以下问题中获取了代码: R中使用小叶的卷积图包装并对其进行调整以适合我的数据
I have taken the code from this question: Choropleth maps in R using leaflet packageand adapted it to suit my data
#Plot the Leaflet map----
mymap <- leaflet() %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
addPolygons(data = coords, #This is data frame of U.K long and lat
fillColor = ~palette(file$TotalByPostcode),
fillOpacity = 0.6,
color = "darkgrey",
weight = 1.5,
popup = popup1)%>%
addLegend(position = 'topleft',
colors = c('#fee0d2',
'#fcbba1',
'#fc9272',
'#fb6a4a',
'#ef3b2c',
'#cb181d',
'#a50f15',
'#67000d'),
labels = c('0',"","","","","","",'100'),
opacity = 0.6,
title = "Totals")
print(map)
这是我的数据框:
推荐答案
我手中没有您的数据.但是我想您误解了有关传单包的一件事.如果在R控制台中键入?leaflet
,则将在帮助页面中看到以下信息.
I do not have your data in my hand. But I guess that you misunderstood one thing about the leaflet package. If you type ?leaflet
in your R Console, you would see the following information in the help page.
这告诉您传单可以采用哪种数据对象.请牢记这一点,让我们检查以下两种情况.
This tells you what kind of data objects leaflet can take. Keeping this point in your mind, and let's check the following two cases.
在这种情况下,我使用栅格数据包下载了SpatialPolygonsDataFrame,该栅格数据包在我的代码中称为UK
.我还创建了一个称为mydf
的虚拟数据框.在addPolygons()
中,我正在使用空间对象.一张地图很好地出来了.
In this case, I downloaded a SpatialPolygonsDataFrame using the raster package, which is called UK
in my code. I also create a dummy data frame called mydf
. In addPolygons()
, I am using a spatial object. A map nicely came out.
library(raster)
library(leaflet)
library(tidyverse)
# Get UK polygon data
UK <- getData("GADM", country = "GB", level = 2)
### Create dummy data
set.seed(111)
mydf <- data.frame(place = unique(UK$NAME_2),
value = sample.int(n = 1000000, size = n_distinct(UK$NAME_2), replace = TRUE))
### Create five colors for fill
mypal <- colorQuantile(palette = "RdYlBu", domain = mydf$value, n = 5, reverse = TRUE)
leaflet() %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
setView(lat = 55, lng = -3, zoom = 6) %>%
addPolygons(data = UK,
stroke = FALSE, smoothFactor = 0.2, fillOpacity = 0.3,
fillColor = ~mypal(mydf$value),
popup = paste("Region: ", UK$NAME_2, "<br>",
"Value: ", mydf$value, "<br>")) %>%
addLegend(position = "bottomright", pal = mypal, values = mydf$value,
title = "UK value",
opacity = 1)
现在让我们看看常规数据帧会发生什么.我不确定您到底拥有什么样的数据.但是,可以肯定的是,它在类中具有tbl_df
.我想确保对象在类中具有tbl_df
.所以我做了以下事情.我将多边形数据(即英国)转换为数据框.让我们检查一下课程.
Now let's see what happens with a regular data frame. I am not sure what kind of data you exactly have. But, one thing for sure is that it has tbl_df
in class. I wanted to make sure that an object has tbl_df
in class. So I did the following. I converted the polygon data (i.e., UK) to a data frame. Let's check the class.
x <- as_data_frame(fortify(UK))
class(x)
#[1] "tbl_df" "tbl" "data.frame"
在继续之前,我想再次提及一下,您不能绘制带有普通数据框的多边形,包括在类中带有tbl_df
的多边形.这就是我在本文开头提到的内容.因此,以下代码(我刚刚将UK
更改为x
)返回了错误消息.
Before we move on, I want to mention again that you cannot draw a polygon with a normal data frame including one with tbl_df
in class. This is what I said at the beginning of this post. Hence, the following code, which I just changed UK
to x
, returned an error message.
leaflet() %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
setView(lat = 55, lng = -3, zoom = 6) %>%
addPolygons(data = x,
stroke = FALSE, smoothFactor = 0.2, fillOpacity = 0.3,
fillColor = ~mypal(mydf$value)) %>%
addLegend(position = "bottomright", pal = mypal, values = mydf$value,
title = "UK value",
opacity = 1)
我希望这个示范能让您头脑清醒.要学习的课程是:使用sp或sf包中的空间类对象.在您的情况下,您可能想在addPolygons()
中使用FILE1
.确保它是一个空间对象.如果文件正确,则会在图块地图的顶部看到多边形.
I hope this demonstration clears up your mind. The lesson to take is: Use a spatial class object from the sp or sf package. In your case, you would perhaps want to use FILE1
in addPolygons()
. Make sure that it is a spatial object. If you have a right file, you will see polygons on top of the tile map.
这篇关于如何在传单Map R上创建一个Choropleth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!