简体   繁体   English

美国地图上点的间隔大小和颜色-离散误差

[英]Interval size and colors for points on USA map - discrete error

library(mapdata)
library(ggplot2)

The data.frame sest is similar to the structure of my actual data set. data.frame sest与我的实际数据集的结构相似。 For each siteid, I have its latitude (y) and longitude (x) along with its discharge value (value) & the id for the site type (either 3313 or 9012 in the factor ID). 对于每个站点ID,我都有其纬度(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)

The first 2 lines for producing the following map are revised from scale bar and north arrow on map-ggplot2 生成以下地图的前两行是从map-ggplot2上的比例尺和向北箭头修改的

基本地图

The map above provides the basic idea that I'm trying to accomplish; 上面的地图提供了我要完成的基本概念; however, I want to specify the Discharge values ("Discharge" replacing "value" in the Legend name) to be in the following range: < 1, 1 - 10, 10 - 20, 20 - 30, and > 30. I also want to specify the colors used for ID. 但是,我要指定“放电”值(“图例名称中的”放电”替换“值”)在以下范围内:<1、1-10、10-20、20-30和>30。我也要指定用于ID的颜色。

I have shown my code below where I have attempted to create the real map that I want. 我在下面的代码中显示了我要创建所需真实地图的位置。 The error message that I have received follows the full R code. 我收到的错误消息遵循完整的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

How should the plot code be revised? 地块代码应如何修改?

This should work: 这应该工作:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM