2016-11-19 74 views
2

我在R中绘制了一个地图,绘制了几个城市的点大小由人口决定;它是叠加在Tirgris/Line Shapefiles上的气泡图。一切工作都很好,除了传说对于可能的数据没有很好的规模。我有几个社区,约800-1100人,一个约2000人,另外两个约25000 +。 ggplot创造的传说有10000,20000和30000人口的泡沫。我希望它是2000,20000,& 30000之类的东西。我做了很多研究,但没有发现任何有希望的东西。修改ggplot2气泡图的图例比例尺

library(ggplot) 
library(rgdal) 
library(tigris) 

cities.data = read.csv("cities.csv", header = TRUE) 
latah.mp <- county_subdivisions(state = '16', county = '057', cb = TRUE)            # reads TIGER/Line data from US Census for Latah County 
whitman.mp <- county_subdivisions(state = '53', county = '075', cb = TRUE)            # reads TIGER/Line data from US Census for Whitman County 
region.mp = rbind_tigris(latah.mp, whitman.mp)                 # binds county shapefiles into one image 
region.map <- fortify(region.mp) 

ggplot() + 
geom_map(data = region.map, map = region.map, aes(x = long, y = lat, map_id = id), fill = "#CCE5CC", color = "#BBD4BB") + 
geom_point(data = cities.data, aes(x = long, y = lat, size = Population), alpha = 0.5, color = "black") + 
scale_size_continuous(range = c(1,10)) + 
annotate("text", x = -117.163454, y = 46.6827, label = "Pullman", size = 2.5) + 
annotate("text", x = -116.998909, y = 46.6827, label = "Moscow", size = 2.5) + 
annotate("text", x = -117.311862, y = 46.8897, label = "Colfax", size = 2.5) + 
annotate("text", x = -116.772232, y = 46.7075, label = "Troy", size = 2.5) + 
annotate("text", x = -117.07534, y = 46.8803, label = "Palouse", size = 2.5) + 
annotate("text", x = -116.92887, y = 46.5215, label = "Genessee", size = 2.5) + 
annotate("text", x = -117.073984, y = 47.1953, label = "Teko", size = 2.5) + 
annotate("text", x = -116.897068, y = 46.8930, label = "Potlatch", size = 2.5) + 
theme_classic() + 
coord_quickmap() + 
theme(axis.line = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank()) 

Map of Whitman & Latah Counties

从文件“cities.csv”中的数据低于是否有帮助,能够全程运行代码。

city   lat  long    Population 
    Pullman.WA  46.7327 -117.1635  30851 
    Tekoa.WA  47.2253 -117.0740  789 
    Palouse.WA  46.9103 -117.0753  1092 
    Colfax.WA  46.9197 -117.3119  2826 
    Potlatch.ID  46.9230 -116.8971  773 
    Moscow.ID  46.7306 -116.9989  24406 
    Troy.ID   46.7375 -116.7722  906 
    Genesee.ID  46.5515 -116.9289  965 
+0

也许'scale_size_continuous(range = c(1,10),breaks = c(2000,20000,30000))' – cuttlefish44

回答

1

我发现有三种潜在的解决方案可以处理类似的数据集。根据您正在使用的特定数据集和条件,您选择的内容可能有所不同:

(1)使用scale_fill_gradient使用不同比例(对数等)。 (可能要玩不同的尺度)

p <- p + scale_fill_gradient(trans = 'log') 

(2)使用的限制之外scale_fill_gradientn的东西都会显示为NA限制你的传奇规模。

p <- p + scale_fill_gradientn(colours=topo.colors(7), 
           breaks=c(0,1000,2000), 
           limits=c(0,5000)) 

(3)通过操纵手动输入到地图中的数据(在这个例子中,我使用的数据集data与你映射为value值)创建自己的离散规模这是最繁琐的解决方案,但也可能如果您的数据是非常特殊的分布式的最佳解决方案

# initial data manipulation before creating map 
data$value[data$value < 1000] <- 0 
data$value[data$value > 1000 & data$value < 2000] <- 1 
data$value[data$value > 2000] <- 2 
data$value <- as.factor(data$value) 
... 
p <- p + scale_fill_manual(values=c("white", "#034e7b","#3690c0"), 
          breaks=c('0','1','2'), 
          labels=c('< 1000', '1000-2000', '>2000'))