我正在尝试创建地图图表与一些餐厅在美国按县。然而,由于垃圾桶的值范围很大,所有的县在颜色上看起来都非常相似。如何自定义箱数,以便向图表中添加更多颜色。
我试图清理代码,但下面可能有一些额外的无关代码行,我正在使用其他viz。
这是我的代码。
##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个垃圾箱组。如何添加更多的垃圾桶或创建自定义垃圾桶。我花了很多时间试图找到这个问题的解决方案,但没有成功。
Update#
我终于找到了我正在寻找的解决方案。我就是这么做的
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上的外观,所以我也可以尝试一下。
这是现在的图表:

发布于 2016-07-17 03:24:52
由于您无论如何都在使用原始计数(而不是更健壮的方法,如对每1K、10K或100K县居民进行归一化),所以只需切换到使用对数刻度(您可以使用专题地图pkg来执行此操作,但这里有一个ggplot2解决方案):
library(albersusa) # devtools::install_github("hrbrmstr/albersusa")
library(readxl)
library(dplyr)
library(maptools)
library(rgeos)
library(ggplot2)
library(ggthemes)
library(ggalt)
library(viridis)
URL <- "http://www.ers.usda.gov/datafiles/Food_Environment_Atlas/Data_Access_and_Documentation_Downloads/Current_Version/DataDownload.xls"
fil <- basename(URL)
if (!file.exists(fil)) download.file(URL, fil)
restaurants <- read_excel(fil, 11)
select(restaurants, FIPS, FFR12, FSR12) %>%
mutate(total=FFR12+FSR12) %>%
select(FIPS, total) -> restaurants
counties <- counties_composite()
us_map <- fortify(counties, region="fips")
gg <- ggplot()
gg <- gg + geom_map(data=us_map, map=us_map,
aes(long, lat, map_id=id),
color="#ffffff00", size=0.05, fill=NA)
gg <- gg + geom_map(data=restaurants, map=us_map,
aes(fill=total, map_id=FIPS),
color="#ffffff", size=0.05)
gg <- gg + scale_fill_viridis(name="Total:", trans="log10")
gg <- gg + coord_proj(us_aeqd_proj)
gg <- gg + theme_map()
gg <- gg + theme(legend.position=c(0.8, 0.25))
gg

但是,请考虑以某种方式将数据标准化。
https://stackoverflow.com/questions/38414614
复制相似问题