不带经纬度的R地图绘图

不带经纬度的R地图绘图,r,dictionary,plot,R,Dictionary,Plot,我想在没有经度和纬度值的情况下绘制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 v

我想在没有经度和纬度值的情况下绘制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

由于缺少可复制的示例,我仅手动键入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()
库(dplyr)
图书馆(GG2)

df这是迪帕扬·萨卡尔(Deepayan Sarkar)在其著作《晶格:绘制一个伪3d条形图》中提供的代码,其中美国大陆各州作为条形图的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)))

state.info这里是一个部分choropleth,使用@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 = "")

df这里有一个
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')
library(plotly)
#创建df,但获取原始海报数据的子集

df此数据集可能有帮助:有一个美国州数据的帮助文件,您可以使用
?state
调出该文件。state.center列表包含州中心的纬度和经度。看起来很不错,但我在运行云时一直遇到这个错误(密度部分:错误:在“panel.3dspatch(…)}中出现意外符号,xlim参数缺少“=”。我修复了它。现在应该可以剪切和粘贴了。