2015-05-21 77 views
1

我想繪製沒有經度和緯度值的R地圖。大多數地圖功能使用經度和緯度值。我所擁有的唯一信息是國家和頻率的名稱。請讓我知道如何繪製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 
+1

此數據集可能有所幫助:https://dev.maxmind.com/geoip/legacy/codes/state_latlon/ – akhmed

+1

有一個美國國家數據的幫助文件,你可以用''state'拉起來。 state.center列表具有狀態中心的經度和緯度。 –

回答

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() 

,給了我這樣的:

enter image description here

你的全部數據幀df應該工作以及。

1

這是Deepayan Sarkar在他的書「Lattice:」中提供的代碼,用於將美國大陸各州的僞3d barplot作爲酒吧的x.y位置。您應該能夠用數據集中的值替換「密度」值。您可能需要刪除排除AK和HI。

state.info <- data.frame(name = state.name, long = state.center$x, lat = state.center$y, 
          area = state.x77[, "Area"], 
          population = 1000 * state.x77[, "Population"]) 
state.info$density <- with(state.info, population/area) 

library("maps") 
state.map <- map("state", plot=FALSE, fill = FALSE) 
panel.3dmap <- function(..., rot.mat, distance, xlim, ylim, zlim, xlim.scaled, 
ylim.scaled, zlim.scaled) { scaled.val <- function(x, original, scaled) { 
       scaled[1] + (x - original[1]) * diff(scaled)/diff(original) } 
m <- ltransform3dto3d(rbind(scaled.val(state.map$x, xlim, xlim.scaled), 
    scaled.val(state.map$y, ylim, ylim.scaled), zlim.scaled[1]), rot.mat, distance) 
panel.lines(m[1,], m[2,], col = "grey76") } 

cloud(density ~ long + lat, state.info, subset = !(name %in% c("Alaska", "Hawaii")), 
panel.3d.cloud = function(...) { panel.3dmap(...) 
           panel.3dscatter(...) }, 
    type = "h", scales = list(draw = FALSE), zoom = 1.1, xlim = state.map$range[1:2], 
    ylim = state.map$range[3:4], xlab = NULL, ylab = NULL, zlab = NULL, 
    aspect = c(diff(state.map$range[3:4])/diff(state.map$range[1:2]), 0.3), 
     panel.aspect = 0.75, lwd = 2, screen = list(z = 30, x = -60), 
     par.settings = list(axis.line = list(col = "transparent"), 
          box.3d = list(col = "transparent", alpha = 0))) 

enter image description here

+0

看起來太棒了,但是我一直在運行雲時出現這個錯誤(密度部分:錯誤:意外符號在: 「panel.3dscatter(...)}, – lawyeR

+0

xlim參數缺少」=「。固定它,現在應該是可切換的。 –

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 = "") 

partial choropleth

你可以用scale_fill_gradient2修改配色方案,例如。

相關問題