简体   繁体   English

如何将数据点中的数据点添加到斯洛伐克行政区域的多边形地图中?

[英]How to add data points from dataframe to polygon map of administrative regions of Slovakia?

pardon me if it is a basic question, this is my first time to write here, so my thanks in advance. 原谅我,如果这是一个基本问题,这是我第一次写这里,所以我提前感谢。

I have exported a report from Google Analytics with columns Longitude, Latitude and Sessions and I want to add these data points to polygon map I have created in R for administrative regions of Slovakia. 我已经从Google Analytics中导出了包含经度,纬度和会话列的报告,我想将这些数据点添加到我在R中为斯洛伐克行政区域创建的多边形地图中。

This is what I have for now. 这就是我现在所拥有的。

##Load the Raster Library
library(raster)

##Get the Province Shapefile for Slovakia
slovakia_level_1 <- getData('GADM', country='SVK', level=1)
slovakia_level_2 <- getData('GADM', country='SVK', level=2)

##Plot this shapefile
plot(slovakia_level_1)

library(ggmap)   ##load the ggmap package so we can access the crime data

## read our dataset with sessions from google analytics ( more on how to read excel files http://www.sthda.com/english/wiki/reading-data-from-excel-files-xls-xlsx-into-r)

library(readxl) ## this is the dataframe from google analytics and i would like to plot these data to the slovakia administrtaive region map
lugera <- read_excel("Analytics 01. [Lugera.sk] - [Reporting View] - [Filtered Data] New Custom Report 20190101-20190627.xlsx")

But i really do not know how to move on. 但我真的不知道如何继续前进。 I went based on this article http://data-analytics.net/wp-content/uploads/2014/09/geo2.html but i have stuck when i needed to plot points. 我基于这篇文章http://data-analytics.net/wp-content/uploads/2014/09/geo2.html,但是当我需要绘制积分时,我已经陷入困境。

This is a sample how the report from google analytics looks like: 以下是Google Analytics分析报告的示例:

Longitude Latitude Sessions
17.1077 48.1486 25963
0.0000  0.0000  13366
21.2611 48.7164 4732
18.7408 49.2194 3154
21.2393 49.0018 2597
18.0335 48.8849 2462
19.1462 48.7363 2121
17.5833 48.3709 1918
18.0764 48.3061 1278
14.4378 50.0755 1099
20.2954 49.0511 715
18.1571 47.9882 663
18.6245 48.7745 653
17.8272 48.5918 620
18.9190 49.0617 542
19.1371 48.5762 464
-6.2603 53.3498 369
18.1700 48.5589 369
20.5637 48.9453 325
-0.1278 51.5074 284
21.9184 48.7557 258

Can someone help me how to progress from here as I am struggling to figure it out how to plot those points on polygon map. 有人可以帮助我如何从这里进步,因为我正在努力弄清楚如何在多边形地图上绘制这些点。

Is it also possible to create a heat map over particular regions as well, please? 是否也可以在特定区域创建热图,好吗?

I hope it was clear, but if not, please tell me, i will improve my question, this is my first time to ask. 我希望很清楚,但如果没有,请告诉我,我会改进我的问题,这是我第一次问。

Thank you very much! 非常感谢你!

UPDATE UPDATE

I was trying to reproduce Jay`s answer and the first map with red dots works awesome! 我试图重现杰伊的答案,第一张带红点的地图真棒! Thanks! 谢谢!

But in case of the heat map I am getting errors and cannot reproduce the same map as I am getting several erros. 但是在热图的情况下,我收到错误,无法重现相同的地图,因为我得到了几个错误。

Belowe is my code how it looks like and I am not sure where is the issue as I tried to name my dataframe as ses the same way as in jay`s answer. 下面是我的代码它是什么样子,我不知道问题出在哪里,因为我试图将我的数据框命名为ses ,就像在jay的回答中一样。

##Load the Raster Library

library(raster)  # imports library(sp)
slovakia_level_1 <- getData('GADM', country='SVK', level=1)


##Plot
plot(slovakia_level_1)
points(coordinates(slovakia_level_2), pch=20, col="red")

#ses is my google analytics dataframe where all 3 columns Longitude, Latitude and Sessions are numeric

## it is imported excel file to r and stored as a dataframe

ses

spdf <- SpatialPointsDataFrame(coords=ses[1:2], data=ses[3], 
                               proj4string=CRS(proj4string(slovakia_level_2)))
ppl.sum <- aggregate(x=spdf["Sessions"], by=slovakia_level_2, FUN=sum)
spplot(ppl.sum, "Sessions", main="Sessions in Slovakia")

These are the errors I am getting 这些是我得到的错误

spdf <- SpatialPointsDataFrame(coords=ses[1:2], data=ses[3], 
+                                proj4string=CRS(proj4string(slovakia_level_2)))
Error in proj4string(slovakia_level_2) : 
  object 'slovakia_level_2' not found
> ppl.sum <- aggregate(x=spdf["Sessions"], by=slovakia_level_2, FUN=sum)
Error in aggregate(x = spdf["Sessions"], by = slovakia_level_2, FUN = sum) : 
  object 'spdf' not found
> spplot(ppl.sum, "Sessions", main="Sessions in Slovakia")
Error in spplot(ppl.sum, "Sessions", main = "Sessions in Slovakia") : 
  object 'ppl.sum' not found

Please, take my huge thanks for being so helpful on my first question and I cannot express my respect to all people at StackOverflow. 请非常感谢您对我的第一个问题非常有帮助,我无法向StackOverflow上的所有人表达我的敬意。

Thank you 谢谢

Actually there's a coordinates() function included in the sp package (imported from raster ), with which we easily can add the points to the plot . 实际上sp包中包含一个coordinates()函数(从raster导入),我们可以使用它轻松地将points添加到plot

library(raster)  # imports library(sp)
slovakia_level_1 <- getData('GADM', country='SVK', level=1)
slovakia_level_2 <- getData('GADM', country='SVK', level=2)

##Plot
plot(slovakia_level_1)
points(coordinates(slovakia_level_2), pch=20, col="red")

在此输入图像描述

To get a heatmap using your google analytics data (here ses ) we can use spplot() , also included in sp . 要使用您的谷歌分析数据(此处为ses )获取热图,我们可以使用spplot() ,也包含在sp First we need to create a SpatialPointsDataFrame , which - according to this post on gis.stackexchange - we aggregate to match ses$Sessions points and polygons from slovakia_level_2 . 首先,我们需要创建一个SpatialPointsDataFrame ,根据SpatialPointsDataFrame上的这篇文章,我们聚合以匹配来自slovakia_level_2 ses$Sessions点和多边形。

spdf <- SpatialPointsDataFrame(coords=ses[1:2], data=ses[3], 
                               proj4string=CRS(proj4string(slovakia_level_2)))
ppl.sum <- aggregate(x=spdf["Sessions"], by=slovakia_level_2, FUN=sum)
spplot(ppl.sum, "Sessions", main="Sessions in Slovakia")

Result 结果

在此输入图像描述

Data 数据

# your data from google analytics above
ses <- structure(list(Longitude = c(17.1077, 0, 21.2611, 18.7408, 21.2393, 
18.0335, 19.1462, 17.5833, 18.0764, 14.4378, 20.2954, 18.1571, 
18.6245, 17.8272, 18.919, 19.1371, -6.2603, 18.17, 20.5637, -0.1278, 
21.9184), Latitude = c(48.1486, 0, 48.7164, 49.2194, 49.0018, 
48.8849, 48.7363, 48.3709, 48.3061, 50.0755, 49.0511, 47.9882, 
48.7745, 48.5918, 49.0617, 48.5762, 53.3498, 48.5589, 48.9453, 
51.5074, 48.7557), Sessions = c(25963L, 13366L, 4732L, 3154L, 
2597L, 2462L, 2121L, 1918L, 1278L, 1099L, 715L, 663L, 653L, 620L, 
542L, 464L, 369L, 369L, 325L, 284L, 258L)), row.names = c(NA, 
-21L), class = "data.frame")

The simplest way to do it would be this ( slov_df is your dataset): 最简单的方法是( slov_df是你的数据集):

library(sp)
library(ggplot2)

slov_reg <- fortify(slovakia_level_2)

ggplot() +
  geom_polygon(data = slov_reg, aes(x = long, y = lat, group = group), col = "black", fill = NA) +
  geom_point(data = slov_df, aes(x = Longitude, y = Latitude))

在此输入图像描述

EDIT : 编辑

Nice solution by jay.sf. jay.sf的好解决方案。 If you like this let me provide another option: 如果你喜欢这个让我提供另一种选择:

sp_google <- SpatialPointsDataFrame(coords=slov_df[1:2], data=slov_df[3], 
                                    proj4string=CRS(proj4string(slovakia_level_2)))


slovakia_level_2@data$Sessions <- over(slovakia_level_2, sp_google, fn = sum)$Sessions
slovakia_level_2@data$id <- row.names(slovakia_level_2@data)
slov_reg <- fortify(slovakia_level_2, region = "id")
slov_reg <- join(slov_reg, slovakia_level_2@data, by="id")


ggplot() +
  geom_polygon(data = slov_reg, aes(x = long, y = lat, group = group, fill = Sessions), col = "black") +
  scale_fill_gradient(low = "yellow",  high = "red", na.value = "lightgrey") +
  theme_bw()

It's a little bit more work, but in the end ggplot offers you a much wider range of customization options. 这是一个更多的工作,但最终ggplot为您提供了更广泛的自定义选项。 It's a question of your preference. 这是你的偏好问题。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM