如何使用 ggplot 绘制美国城市?
How do I plot US cities using ggplot?
我可以很好地绘制路易斯安那州...
require(ggplot2)
require(ggmap)
require(maps)
LA <- map_data("state", region="louisiana")
ggplot(LA, aes(x=long, y=lat))+geom_polygon()
现在,我有关于洛杉矶特定城市的销售电话数量的数据。我如何为该地块进行销售拜访的每个城市添加一个点?
salesCalls <- data.frame(State=rep("louisiana",5),
City=c("Baton Rouge", "New Orleans", "Shreveport", "Lafayette", "Mandeville"),
Calls=c(10,5,8,13,2))
salesCalls
State City Calls
1 louisiana Baton Rouge 10
2 louisiana New Orleans 5
3 louisiana Shreveport 8
4 louisiana Lafayette 13
5 louisiana Mandeville 2
require(ggplot2)
require(ggmap)
require(maps)
LA <- map_data("state", region="louisiana")
salesCalls <- data.frame(State=rep("louisiana",5),
City=c("Baton Rouge", "New Orleans", "Shreveport",
"Lafayette", "Mandeville"),
Calls=c(10,5,8,13,2))
salesCalls <- cbind(geocode(as.character(salesCalls$City)), salesCalls)
salesCalls
# lon lat State City Calls
# 1 -91.14032 30.45828 louisiana Baton Rouge 10
# 2 -90.07153 29.95107 louisiana New Orleans 5
# 3 -93.75018 32.52515 louisiana Shreveport 8
# 4 -92.01984 30.22409 louisiana Lafayette 13
# 5 -90.06563 30.35825 louisiana Mandeville 2
ggplot(LA, aes(x=long, y=lat)) +
geom_polygon() +
coord_map() +
geom_point(data=salesCalls, aes(x=lon, y=lat, size=Calls), color="orange")
在 Google 地图上:
ggmap(get_map(location = 'Louisiana', zoom = 7)) +
geom_point(data=salesCalls, aes(x=lon, y=lat, size=Calls), color="orange")
以下脚本应该适用于 google 地图部分:
ggmap(get_map(location = 'Louisiana', zoom = 7)) +
geom_point(
data = salesCalls,
aes(x = salesCalls$lon, y = salesCalls$lat, size = salesCalls$Calls),
color = "orange"
)
我可以很好地绘制路易斯安那州...
require(ggplot2)
require(ggmap)
require(maps)
LA <- map_data("state", region="louisiana")
ggplot(LA, aes(x=long, y=lat))+geom_polygon()
现在,我有关于洛杉矶特定城市的销售电话数量的数据。我如何为该地块进行销售拜访的每个城市添加一个点?
salesCalls <- data.frame(State=rep("louisiana",5),
City=c("Baton Rouge", "New Orleans", "Shreveport", "Lafayette", "Mandeville"),
Calls=c(10,5,8,13,2))
salesCalls
State City Calls
1 louisiana Baton Rouge 10
2 louisiana New Orleans 5
3 louisiana Shreveport 8
4 louisiana Lafayette 13
5 louisiana Mandeville 2
require(ggplot2)
require(ggmap)
require(maps)
LA <- map_data("state", region="louisiana")
salesCalls <- data.frame(State=rep("louisiana",5),
City=c("Baton Rouge", "New Orleans", "Shreveport",
"Lafayette", "Mandeville"),
Calls=c(10,5,8,13,2))
salesCalls <- cbind(geocode(as.character(salesCalls$City)), salesCalls)
salesCalls
# lon lat State City Calls
# 1 -91.14032 30.45828 louisiana Baton Rouge 10
# 2 -90.07153 29.95107 louisiana New Orleans 5
# 3 -93.75018 32.52515 louisiana Shreveport 8
# 4 -92.01984 30.22409 louisiana Lafayette 13
# 5 -90.06563 30.35825 louisiana Mandeville 2
ggplot(LA, aes(x=long, y=lat)) +
geom_polygon() +
coord_map() +
geom_point(data=salesCalls, aes(x=lon, y=lat, size=Calls), color="orange")
在 Google 地图上:
ggmap(get_map(location = 'Louisiana', zoom = 7)) +
geom_point(data=salesCalls, aes(x=lon, y=lat, size=Calls), color="orange")
以下脚本应该适用于 google 地图部分:
ggmap(get_map(location = 'Louisiana', zoom = 7)) +
geom_point(
data = salesCalls,
aes(x = salesCalls$lon, y = salesCalls$lat, size = salesCalls$Calls),
color = "orange"
)