R ggplot2 / ggmap同心圆为点 [英] R ggplot2/ggmap concentric circles as points
问题描述
我试图绘制一些显示全部人口的信息,然后根据地图上的位置绘制一部分人口。我已经看到使用同心圆或三维倒锥体传达这一点的数据可视化。我只是无法弄清楚如何在 ggplot
/ ggmap
下面是 Paint
中的免费版本,它显示了我正在做的事情的一个大概概念:
以下是一个粗略的例子:
> dput(df1)
structure(list(zip = c(00210,00653,00952,02571,04211,
05286,06478,07839 ,10090,11559),city = c(Portsmouth,
Guanica,Sabana Seca,Wareham,Auburn,Craftsbury,
Oxford ,Greendell,纽约,劳伦斯),州= c(NH,
PR,PR,MA,ME,VT,CT ,NJ,NY,NY),纬度= c(43.005895,
17.992112,18.429218,41.751554,44.197009,44.627698,41.428163,
41.12831,40.780751,40.61579),经度= c(-71.013202,-66.90097,
-66.18014,-70.71059,-70.239485,-72.434398,-73.12729,-74.678956,
-73.977182,-73.73126),时区= c(-5L,-4L ,-4L,-5L,-5L,
-5L,-5L,-5L,-5L,-5L),dst = c(TRUE,FALSE,FALSE,TRUE,TRUE,
TRUE, TRUE,TRUE,TRUE,TRUE),totalPop = c(43177,37224,37168,
15492,1614,88802,2587,80043,78580,87461),subPop = c(42705,
36926, 27556,10827,774,39060,1542,21304,53438,2896)),.Names = c(zip,
city,state,latitu (1L,50L,200L,900L,1500L,
2000L,b), 2500L,3000L,3500L,4000L),class =data.frame)
有什么建议? / p>
基本思想是对两个群体使用单独的geoms,确保较小的群体在较大的群体之后绘制它的图层位于顶层:
库(ggplot2)#使用版本0.9.2.1
库(地图)
#加载我们地图数据
all_states< - map_data(state)
#启动ggplot。它不会绘制直到我们输入p
p < - ggplot()
#将美国状态轮廓添加到ggplot
p < - p + geom_polygon(data = all_states,aes (x = long,y = lat,group = group),
color =gray,fill =white)
#添加总人口
p < - p + geom_point(data = df1,aes(x = longitude,y = latitude,size = totalPop),
color =#b5e521)
#add sub作为单独图层的群体, (b = b,b,b,b,b,b,b,b,b),geom_point(data = df1,aes(x = longitude,y = latitude,size = subPop),
color =#00a3e8)
#将图例名称更改为通用词人口
p < - p + guides(size = guide_legend(title =Population))
#显示图
p
get_map()来自 ggmap
包提供了几个选项:
require(ggmap)
require(mapproj)
map< - get_map(location ='united states',zoom = 3,maptype =terrain,
source =google)
p < - ggmap(map)
在此之后,您添加总体和子群体geom_point()图层,并像以前一样显示它。
I am trying to plot some information that shows full population and then a subset of that population by location on a map. I've seen data visualizations that use concentric circles or 3-d inverted cones to convey this. I just can't figure out how to do it in ggplot
/ ggmap
Here's a free hand version in Paint
that shows a rough idea of what I'm looking to do:
Here's a rough piece of data for an example:
> dput(df1)
structure(list(zip = c("00210", "00653", "00952", "02571", "04211",
"05286", "06478", "07839", "10090", "11559"), city = c("Portsmouth",
"Guanica", "Sabana Seca", "Wareham", "Auburn", "Craftsbury",
"Oxford", "Greendell", "New York", "Lawrence"), state = c("NH",
"PR", "PR", "MA", "ME", "VT", "CT", "NJ", "NY", "NY"), latitude = c(43.005895,
17.992112, 18.429218, 41.751554, 44.197009, 44.627698, 41.428163,
41.12831, 40.780751, 40.61579), longitude = c(-71.013202, -66.90097,
-66.18014, -70.71059, -70.239485, -72.434398, -73.12729, -74.678956,
-73.977182, -73.73126), timezone = c(-5L, -4L, -4L, -5L, -5L,
-5L, -5L, -5L, -5L, -5L), dst = c(TRUE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE), totalPop = c(43177, 37224, 37168,
15492, 1614, 88802, 2587, 80043, 78580, 87461), subPop = c(42705,
36926, 27556, 10827, 774, 39060, 1542, 21304, 53438, 2896)), .Names = c("zip",
"city", "state", "latitude", "longitude", "timezone", "dst",
"totalPop", "subPop"), row.names = c(1L, 50L, 200L, 900L, 1500L,
2000L, 2500L, 3000L, 3500L, 4000L), class = "data.frame")
Any suggestions?
The basic idea is to use separate geoms for the two populations, making sure the smaller one is plotted after the larger one, so its layer is on top:
library(ggplot2) # using version 0.9.2.1
library(maps)
# load us map data
all_states <- map_data("state")
# start a ggplot. it won't plot til we type p
p <- ggplot()
# add U.S. states outlines to ggplot
p <- p + geom_polygon(data=all_states, aes(x=long, y=lat, group = group),
colour="grey", fill="white" )
# add total Population
p <- p + geom_point(data=df1, aes(x=longitude, y=latitude, size = totalPop),
colour="#b5e521")
# add sub Population as separate layer with smaller points at same long,lat
p <- p + geom_point(data=df1, aes(x=longitude, y=latitude, size = subPop),
colour="#00a3e8")
# change name of legend to generic word "Population"
p <- p + guides(size=guide_legend(title="Population"))
# display plot
p
From the map, it is clear your data include non-contiguous-US locations, in which case you may want different underlying map data. get_map()
from ggmap
package provides a couple options:
require(ggmap)
require(mapproj)
map <- get_map(location = 'united states', zoom = 3, maptype = "terrain",
source = "google")
p <- ggmap(map)
After which you add the total and sub Population geom_point() layers and display it as before.
这篇关于R ggplot2 / ggmap同心圆为点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!