我正在尝试按县创建包含美国多家餐厅的地图。然而,由于垃圾箱的价值范围很大,所有县的颜色看起来都非常相同。如何自定义箱的数量,以便为图表添加更多颜色。
我试图清理代码,但下面可能有几行额外的无关代码,我用于其他可视化。
这是我的代码。
##some libraries are not used
library(rgeos)
library(rgdal)
library(maptools)
library(readxl)
library(tmap)
library(gpclib)
#reading data from excel file
#source of the file http://www.ers.usda.gov/datafiles/Food_Environment_Atlas/Data_Access_and_Documentation_Downloads/Current_Version/DataDownload.xls . I am using local file in my computer
data_restaurant <- read_excel(...)
#reading shapes to drawn on the map chart
# I am reading from local file but the actual source is http://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_050_00_20m.zip
us_shape <- read_shape(..)
#Removing Alaska, Hawaii and Puertorico
us_shape <- us_shape[!(us_shape$STATE %in% c("02","15","72")),]
#assign data to shape
us_shape$FIPS <- paste0(us_shape$STATE, us_shape$COUNTY)
us_shape <- append_data(us_shape, data, key.shp = "FIPS", key.data = "FIPS")
restaurant_shape <- append_data(us_shape, data_restaurant, key.shp = "FIPS", key.data = "FIPS")
#draw the map
draw_map_adult_obs_2010 <- qtm(us_shape, fill = "PCT_OBESE_ADULTS10", fill.palette="Reds",title="2010 Adult Obesity by County, percent",title.position = c("center", "top"))
##This didn't work because of gpclib library not working
##US_states <- unionSpatialPolygons(us_shape, IDs=us_shape$STATE)
#Draw chart restaurant
tm_shape(restaurant_shape, projection="+init=epsg:2163") +
tm_polygons("FFR12", border.col = "grey30", title="", palette="Reds") +
tm_borders(lwd=2, col = "black", alpha = .5) +
tm_layout(title="2012 # of Restaurants by County in USA",
title.position = c("center", "top"),
legend.text.size=0.7)
这是地图图表现在的样子。从图例中可以看出,只有 4 个 bin 组。如何添加更多垃圾箱或创建自定义垃圾箱。我花了很多时间试图找到一个没有运气的解决方案。
更新#
我终于能够找到我正在寻找的解决方案。这就是我所做的
tm_shape(restaurant_shape,projection="+init=epsg:2163") +
tm_fill("FFR12", title = "", style = "fixed",
breaks = c(0, 50, 150, 250, 500,1000,1500, Inf),
palette = "Blues") +
tm_borders() +
tm_layout(title="2012 # of Restaurants by County in USA",
title.position = c("center", "top"),
legend.text.size=0.7)
我喜欢图表在 ggplot2 上的外观,所以我也可以尝试这种方式。