我想绘制没有经度和纬度值的R地图。大多数地图函数都使用经度和纬度值。我拥有的唯一信息是州名和频率。请让我知道如何绘制R地图。
state freq
1 california 14717
2 texas 6842
3 new york 6729
4 florida 6720
5 illinois 5921
6 NA 5897
7 georgia 5008
8 ohio 4197
9 michigan 3593
10 virginia 3278
11 new jersey 3097
12 north carolina 3084
13 washington 3048
14 pennsylvania 2972
15 maryland 2821
16 missouri 2615
17 minnesota 2318
18 massachusetts 2242
19 colorado 2210
20 indiana 2078
21 arizona 1901
22 wisconsin 1842
23 oregon 1817
24 tennessee 1737
25 alabama 1679
26 connecticut 1627
27 south carolina 1122
28 nevada 1090
29 kansas 1062
30 kentucky 983
31 oklahoma 971
32 louisiana 954
33 utah 877
34 arkansas 855
35 mississippi 787
36 nebraska 674
37 idaho 599
38 new hampshire 551
39 new mexico 472
40 rhode island 435
41 hawaii 409
42 west virginia 391
43 montana 330
44 delaware 300
45 vermont 207
46 alaska 200
47 south dakota 189
48 iowa 186
49 wyoming 150
50 maine 101
51 north dakota 52
答案 0 :(得分:1)
缺乏一个可重复的例子,我手动输入了4个状态作为插图:
library(dplyr)
library(ggplot2)
df <- data.frame( state = c("california","texas","nevada","north dakota"),
freq = c(14717, 6842, 1090, 52),
stringsAsFactors = FALSE )
state_level_df <- data.frame(state = tolower(state.name),
long = state.center$x,
lat = state.center$y,
stringsAsFactors = FALSE) %>%
inner_join( df, by="state" )
ggplot(state_level_df, aes(long, lat)) +
borders("state") +
geom_point(aes(color=freq,size=freq), show_guide=FALSE) +
theme(text=element_text(size=18)) +
scale_size(range=c(2,20)) +
scale_color_continuous(low="red",high="green") +
theme_bw()
给了我这个:
您的完整数据框df
也应该有用。
答案 1 :(得分:1)
这是Deepayan Sarkar在他的书中提供的代码&#34; Lattice:&#34;将美国大陆州的伪3d条形图绘制为条形图的x.y位置。您应该能够替换密度&#39;值与数据集中的值。您可能需要删除AK和HI的排除。
{{1}}
答案 2 :(得分:1)
这是一个部分等值区,使用@akhmed提供的部分数据框。
df <- data.frame( state = c("california","texas","nevada","north dakota", rep("NA", 47)),
freq = c(14717, 6842, 1090, 52, rep(0, 47)),
stringsAsFactors = FALSE )
library(maps)
library(ggthemes)
states_map <- map_data("state", region = c("california","texas","nevada","north dakota"))
new_map <- merge(states_map, df, by.x = "region", by.y = "state")
new_map <- arrange(new_map, group, order) # to sort polygons in right order
ggplot(new_map, aes(x = long, y = lat, group = group, fill = freq)) +
geom_polygon(color = "black") +
coord_map("polyconic") + theme_tufte() + labs(x = "", y = "")
例如,您可以使用scale_fill_gradient2
修改配色方案。
答案 3 :(得分:0)
这里是plotly
的替代方案,它使用了以前受访者提供的一些技术:
library(plotly)
# create df but taking a subset of original poster's data
df <- data.frame(state = c("california","texas","nevada","north dakota", rep("NA", 47)),
freq = c(14717, 6842, 1090, 52, rep(0, 47)),
stringsAsFactors = FALSE )
# generate location information for all states (using built-in data)
state.info <- inner_join(data.frame(state=tolower(state.name),
long=state.center$x, lat=state.center$y,
stringsAsFactors=FALSE),
data.frame(state=tolower(datasets::state.name),
abbrev=datasets::state.abb))
# join the test data to the states location info
map.df <- inner_join(state.info, df, by="state")
# set up plotly to zoom in to US only
g <- list(scope='usa', projection=list(type='albers usa'),
showlakes=TRUE, lakecolor=toRGB('white'))
# plot on the US map
plot_ly(map.df, type='choropleth', locationmode='USA-states',
locations=map.df$abbrev, z=map.df$freq, text=map.df$state) %>%
layout(geo=g, title='Frequency by State')
这将产生: