Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用geom_点在R中绘制人口密度图_R_Plot_Ggplot2 - Fatal编程技术网

用geom_点在R中绘制人口密度图

用geom_点在R中绘制人口密度图,r,plot,ggplot2,R,Plot,Ggplot2,我正在做一个简单的加拿大人口密度图。我有基于邮政编码和纬度/经度的人口数据 我想改进绘图,以显示点密度增加时的颜色变化。这是我的代码: library(maptools) library(gdata) library(RColorBrewer) library(classInt) library(maps) library(ggplot2) library(ggmap) Canada <- get_map(location="Canada", zoom=3, maptype="terra

我正在做一个简单的加拿大人口密度图。我有基于邮政编码和纬度/经度的人口数据

我想改进绘图,以显示点密度增加时的颜色变化。这是我的代码:

library(maptools)
library(gdata)
library(RColorBrewer)
library(classInt)
library(maps)
library(ggplot2)
library(ggmap)

Canada <- get_map(location="Canada", zoom=3, maptype="terrain")

ggmap(Canada, extent="normal") + geom_point(data=PopDensity, aes(x=LONG,y=LAT,size=POPULATION), 
alpha = 0.3)+scale_size_continuous(range=c(1,6))
库(maptools)
图书馆(gdata)
图书馆(RColorBrewer)
图书馆(classInt)
图书馆(地图)
图书馆(GG2)
图书馆(ggmap)

加拿大您必须首先计算密度,然后将值指定给点,以便可以映射该点:

library(ggplot2)
library(ggthemes)
library(scales)
library(ggmap)
library(MASS)
library(sp)
library(viridis)

pop <- read.csv("~/Dropbox/PopulationDensity.csv", header=TRUE, stringsAsFactors=FALSE)

# get density polygons
dens <- contourLines(
    kde2d(pop$LONG, pop$LAT, 
          lims=c(expand_range(range(pop$LONG), add=0.5),
                 expand_range(range(pop$LAT), add=0.5))))

# this will be the color aesthetic mapping
pop$Density <- 0

# density levels go from lowest to highest, so iterate over the
# list of polygons (which are in level order), figure out which
# points are in that polygon and assign the level to them

for (i in 1:length(dens)) {
  tmp <- point.in.polygon(pop$LONG, pop$LAT, dens[[i]]$x, dens[[i]]$y)
  pop$Density[which(tmp==1)] <- dens[[i]]$level
}

Canada <- get_map(location="Canada", zoom=3, maptype="terrain")

gg <- ggmap(Canada, extent="normal")
gg <- gg + geom_point(data=pop, aes(x=LONG, y=LAT, color=Density))
gg <- gg + scale_color_viridis()
gg <- gg + theme_map()
gg <- gg + theme(legend.position="none")
gg
库(ggplot2)
图书馆(主题)
图书馆(比例尺)
图书馆(ggmap)
图书馆(弥撒)
图书馆(sp)
图书馆(绿色)

pop在哪里考虑总体值,而不仅仅是长的横向坐标。这不只是表示点的密集程度,而不是每个点的人口值吗?