我做了以下情节:
我使用了这段代码:
library(XML)
library(ggplot2)
library(scales)
library(plyr)
library(maps)
unemp <-
readHTMLTable('http://www.bls.gov/web/laus/laumstrk.htm',
colClasses = c('character', 'character', 'numeric'))[[2]]
names(unemp) <- c('rank', 'region', 'rate')
unemp$region <- tolower(unemp$region)
us_state_map <- map_data('state')
map_data <- merge(unemp, us_state_map, by = 'region')
map_data <- arrange(map_data, order)
states <- data.frame(state.center, state.abb)
p1 <- ggplot(data = map_data, aes(x = long, y = lat, group = group))
p1 <- p1 + geom_polygon(aes(fill = cut_number(rate, 5)))
p1 <- p1 + geom_path(colour = 'gray', linestyle = 2)
p1 <- p1 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette = 'Set1')
p1 <- p1 + coord_map()
p1 <- p1 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p1 <- p1 + theme_bw()
p1
我想在一些操作之后显示scale_fill_brewer
自定义标签,例如将每个标签乘以100或1000之后。
答案 0 :(得分:3)
虽然ggplot助手函数很方便,但我更喜欢在绘图成语之外进行数据争论。这使用cut
为自定义标签添加1%的中断,并使用正确的美国投影,并且具有更适合此数据的顺序色标,并且(最终)删除了大量的图表垃圾:
library(XML)
library(ggplot2)
library(scales)
library(plyr)
library(maps)
library(ggthemes)
unemp <-
readHTMLTable('http://www.bls.gov/web/laus/laumstrk.htm',
colClasses = c('character', 'character', 'numeric'))[[2]]
names(unemp) <- c('rank', 'region', 'rate')
unemp$region <- tolower(unemp$region)
# gain granular control of the breaks outside of ggplot
unemp$brk <- cut(unemp$rate, 0:9, labels=c("0%", sprintf("≥%d%%", 1:8)))
head(unemp)
## rank region rate brk
## 1 1 nebraska 2.6 ≥2%
## 2 2 north dakota 3.1 ≥3%
## 3 3 utah 3.5 ≥3%
## 4 4 vermont 3.6 ≥3%
## 5 5 iowa 3.8 ≥3%
## 6 5 minnesota 3.8 ≥3%
us_state_map <- map_data('state')
map_data <- merge(unemp, us_state_map, by = 'region')
map_data <- arrange(map_data, order)
states <- data.frame(state.center, state.abb)
p1 <- ggplot(data = map_data, aes(x = long, y = lat, group = group))
p1 <- p1 + geom_polygon(aes(fill = brk))
p1 <- p1 + geom_path(colour = 'black', linestyle = 2, size=0.25)
# use a sequential color scale since that's appropriate for the data
p1 <- p1 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette = 'YlGnBu')
# use a proper projection for the U.S.
p1 <- p1 + coord_map("albers", lat0=39, lat1=45)
p1 <- p1 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p1 <- p1 + labs(x=NULL, y=NULL)
p1 <- p1 + theme_map()
p1 <- p1 + theme(legend.position="right")
p1
你还需要处理阿拉斯加&amp; amp;夏威夷。