library(mapdata)
library(ggplot2)
data.frame
sest 类似于我实际数据集的结构。对于每个siteid,我有它的纬度(y)和经度(x)以及它的放电值(值)&站点类型的ID(因子ID中的3313或9012)。
sest <- structure(list(siteid = c("tn1", "tn2", "tn3", "tn4", "tn5",
"tn6", "tn7", "tn8", "tn9", "tn10"), value = c(27.4177804, 0.681706,
0.5773562, 16.9055043, 6.7921217, 29.1585825, 3.7235252, 32.9061032,
11.0193545, 9.1561578), ID = structure(c(1L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 1L), .Label = c("3313", "9012"), class = "factor"),
x = c(-117.850741187576, -131.103264654521, -118.308210170362,
-78.1155254854821, -83.6506382236257, -102.284060011152,
-84.2474668263458, -111.359446789138, -96.1721648671664,
-79.8908686544746), y = c(40.4807797027752, 43.5143695573788,
40.3879383672029, 41.1744817113504, 44.7975007991772, 43.9755006495398,
41.9328322575893, 44.8813332372811, 40.4764496104326, 42.909506658325
)), .Names = c("siteid", "value", "ID", "x", "y"), row.names = c(NA,
-10L), class = "data.frame")
# siteid value ID x y
# 1 tn1 27.4177804 3313 -117.85074 40.48078
# 2 tn2 0.6817060 9012 -131.10326 43.51437
# 3 tn3 0.5773562 3313 -118.30821 40.38794
# 4 tn4 16.9055043 9012 -78.11553 41.17448
# 5 tn5 6.7921217 9012 -83.65064 44.79750
# 6 tn6 29.1585825 9012 -102.28406 43.97550
# 7 tn7 3.7235252 9012 -84.24747 41.93283
# 8 tn8 32.9061032 9012 -111.35945 44.88133
# 9 tn9 11.0193545 3313 -96.17216 40.47645
# 10 tn10 9.1561578 3313 -79.89087 42.90951
usa_map <- data.frame(map("worldHires", "USA")[c("x", "y")])
p <- ggplot(usa_map, aes(x, y)) + geom_path()
p <- p + geom_point(data = sest, aes(x = x, y = y, size = value, color = ID))
print(p)
生成以下地图的前2行是修改自 scale bar and north arrow on map-ggplot2
上面的地图提供了我想要完成的基本想法;但是,我想在下列范围内指定放电值(&#34;放电&#34;替换&#34;值&#34;在图例名称中):&lt; 1,1-10,10-20,20-30,&gt; 30.我还想指定用于ID的颜色。
我已经在下面显示了我的代码,我试图创建我想要的真实地图。我收到的错误消息遵循完整的R代码。
p <- ggplot(usa_map, aes(x, y)) + geom_path()
p <- p + geom_point(data = sest, aes(x = x, y = y, size = value, color = ID))
+ scale_y_continuous(breaks = c(1, 10, 20, 30, 200)) +
scale_size_manual("Discharge", breaks = c("1", "10", "20", "30", "200"),
labels = c("< 1", "1 - 10", "10 - 20", "20 - 30", "> 30"), values = "value")
+ scale_color_manual("ID", values = c("3313" = "purple", "9012" = "green"),
labels = c("All Other IDs", "ID = 9012"))
print(p)
# Error: Continuous value supplied to discrete scale
如何修改情节代码?
答案 0 :(得分:2)
这应该有效:
ggplot(usa_map, aes(x, y)) +
geom_path() +
geom_point(data = sest, aes(x = x, y = y, size = value, color = ID)) +
scale_size("Discharge",
breaks = c(1, 10, 20, 30, 200),
labels = c("< 1", "1 - 10", "10 - 20", "20 - 30", "> 30")) +
scale_color_manual("ID", values = c("3313" = "purple", "9012" = "green"),
labels = c("All Other IDs", "ID = 9012"))