1

我正在尝试按县创建包含美国多家餐厅的地图。然而,由于垃圾箱的价值范围很大,所有县的颜色看起来都非常相同。如何自定义箱的数量,以便为图表添加更多颜色。

我试图清理代码,但下面可能有几行额外的无关代码,我用于其他可视化。

这是我的代码。

##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 上的外观,所以我也可以尝试这种方式。

这是现在图表的样子: 在此处输入图像描述

4

1 回答 1

0

由于您无论如何都在使用原始计数(与更强大的方法相比,例如对每 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

在此处输入图像描述

但是,请考虑以某种方式规范化数据。

于 2016-07-16T19:24:51.580 回答