【问题标题】:Interval size and colors for points on USA map - discrete error美国地图上点的间隔大小和颜色 - 离散误差
【发布时间】:2015-03-23 17:14:12
【问题描述】:
library(mapdata)
library(ggplot2)

data.framesest 与我的实际数据集的结构类似。对于每个 siteid,我都有它的纬度 (y) 和经度 (x) 以及它的流量值 (value) 和站点类型的 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

上面的地图提供了我想要完成的基本想法;但是,我想指定放电值(“放电”替换图例名称中的“值”)在以下范围内: 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

情节代码应该如何修改?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这应该可行:

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

    【讨论】:

    • 感谢您的回答。代码建议完美!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2018-11-25
    • 1970-01-01
    相关资源
    最近更新 更多