修改ggplot2气泡图

时间:2016-11-19 06:33:54

标签: r plot ggplot2

我正在用R绘制地图,绘制点数量由人口决定的几个城市;它是叠加在Tirgris / Line Shapefiles上的气泡图。一切都很好,除了传说中的数据没有很好的规模。我有几个社区,有大约800-1100人,一个有~2000,有2个~25000 +。 ggplot创建的传奇有10000,20000和30000人口的气泡。我希望它像2000,20000和& 30000.我做了很多研究,但没有找到任何有希望的东西。

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

1 个答案:

答案 0 :(得分: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'))