如何在R中绘制美国州?

时间:2014-04-21 22:06:34

标签: r

我可以在整个美国地图中绘制特定州的数据。但我想只绘制状态图和数据(俄克拉荷马州)。

我怎样才能在R?

中完成
ggplot() + 
 geom_polygon( data=all_states, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
 geom_point(data=stations,aes(x=long,y=lat),,colour="red",)+
 ggtitle("Distribution of Flash Flood Events in CONUS")+
 xlab('Longitude')+
 ylab('Latitude')



dput(stations)
structure(list(coop = c(340017L, 340179L, 340256L, 340292L, 340548L, 
340593L, 340908L, 341243L, 341504L, 341724L, 341828L, 342678L, 
342912L, 342944L, 343497L, 343628L, 343821L, 343871L, 344055L, 
344204L, 344235L, 344298L, 344573L, 344766L, 344861L, 345063L, 
345509L, 345779L, 345855L, 346130L, 346139L, 346278L, 346629L, 
346638L, 346670L, 346926L, 346935L, 347012L, 347254L, 348501L, 
348677L, 349395L, 349422L, 349445L), lat = c(34.7864, 34.5903, 
34.2208, 34.1714, 36.7683, 36.8125, 36.7236, 36.8003, 35.1756, 
36.7747, 36.3225, 34.0003, 36.4194, 35.2164, 35.6267, 36.5914, 
35.8161, 35.585, 36.0942, 34.9894, 35.0567, 36.8589, 36.7222, 
36.9031, 35.8583, 34.6097, 34.8911, 35.505, 36.8833, 35.7781, 
36.2283, 36.8914, 36.1217, 35.4253, 35.6239, 34.7253, 36.6692, 
36.2886, 35.0539, 36.1175, 35.9369, 34.1747, 35.52, 35.4814), 
    long = c(-96.685, -99.3344, -95.615, -97.1294, -96.0261, 
    -100.5308, -102.4806, -99.6403, -98.5794, -98.3583, -95.5808, 
    -96.3686, -97.8747, -99.8628, -98.3225, -101.6181, -97.395, 
    -99.3953, -97.835, -99.0525, -96.3861, -101.2172, -97.7903, 
    -102.965, -97.9294, -98.4572, -99.5017, -96.9767, -94.8833, 
    -95.3339, -99.17, -97.0586, -98.315, -96.3033, -96.025, -97.2814, 
    -96.3472, -97.2897, -94.6264, -97.095, -94.9644, -97.9964, 
    -98.6986, -95.2039), elev = c(309.4, 420.6, 143.3, 268.2, 
    217.9, 751.3, 1259.7, 588.3, 451.4, 359.7, 179.2, 182.9, 
    379.5, 627.9, 487.7, 1008.9, 338.3, 554.7, 357.8, 474.3, 
    260.6, 912.9, 318.5, 1325.9, 320, 350.5, 486.2, 281.9, 245.4, 
    157.9, 576.1, 347.5, 370.3, 285, 197.2, 286.5, 254.5, 312.4, 
    134.1, 272.8, 259.1, 278, 493.2, 167.6), state = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "OK", class = "factor"), 
    name = structure(1:44, .Label = c("ADA", "ALTUS IRIG RSCH STN", 
    "ANTLERS", "ARDMORE", "BARTLESVILLE MUNI AP", "BEAVER", "BOISE CITY 2 E", 
    "BUFFALO 2 SSW", "CARNEGIE 5 NE", "CHEROKEE", "CLAREMORE 2 ENE", 
    "DURANT", "ENID", "ERICK", "GEARY", "GOODWELL RSCH STN", 
    "GUTHRIE 5S", "HAMMON 3 SSW", "HENNESSEY 4 ESE", "HOBART MUNI AP", 
    "HOLDENVILLE 2SSE", "HOOKER", "JEFFERSON", "KENTON", "KINGFISHER", 
    "LAWTON", "MANGUM", "MEEKER 5 W", "MIAMI", "MUSKOGEE", "MUTUAL", 
    "NEWKIRK 1NW", "OKEENE", "OKEMAH", "OKMULGEE WTR WKS", "PAULS VALLEY 4 WSW", 
    "PAWHUSKA", "PERRY", "POTEAU WTR WKS", "STILLWATER 2 W", 
    "TAHLEQUAH", "WAURIKA", "WEATHERFORD", "WEBBERS FALLS 5 WSW"
    ), class = "factor")), .Names = c("coop", "lat", "long", 
"elev", "state", "name"), class = "data.frame", row.names = c(NA, 
-44L))

3 个答案:

答案 0 :(得分:6)

您可以使用maps包。地图的外观有很多简单的选项。这是两个非常基本的。就这个问题而言,maps包与ggplot2完全不同,除了在maps中更容易编码的事实。

> library(maps)
> par(mfrow = c(1, 2))
> map('state', region = 'Oklahoma')
> map('county', region = 'Oklahoma')

enter image description here

添加: 由于您添加了数据,因此这是一个夸张的图表,假设数据被称为dat

> map('county', region = 'Oklahoma')
> with(dat, points(lat ~ long, pch = 19, col = 'red'))

enter image description here

答案 1 :(得分:6)

修改的。根据评论更新

由于Geekuna正在使用ggplot,我正在回答ggplot

 m = map_data('state', region = 'Oklahoma')

 ggplot() + 
 geom_polygon( data=m, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
 geom_point(data=stations,aes(x=long,y=lat),,colour="red",)+
 geom_text(data=stations, aes(x=long, y=lat,label=name), size=2, hjust=-0.1) +
 ggtitle("Distribution of Flash Flood Events in CONUS")+
 xlab('Longitude')+
 ylab('Latitude')+
 coord_fixed()

enter image description here

答案 2 :(得分:4)

如果您想要显示县的州,也可以使用ggcounty包。

library(devtools)
install_github(repo="hrbrmstr/ggcounty")
library(ggcounty)

ok <- ggcounty("Oklahoma")

ok$gg

enter image description here

在OP输入后编辑

ok$gg +
  geom_point(data=stations,aes(x=long,y=lat),,colour="red",size=5)+
  ggtitle("Distribution of Flash Flood Events in CONUS")+
  xlab('Longitude')+
  ylab('Latitude')

输出如下: enter image description here

您可能想要删除xlab和ylab。我希望它对你有帮助。