R语言 仅Map图例显示在ggplot中

plicqrtu  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(230)

我试图在Map上绘制数据。但是,只有图例出现在空白图形上。数据集。

structure(list(States.Uts = c("Jammu and Kashmir", "Karnataka", 
"Odisha", "Sikkim", "Madhya Pradesh", "Maharashtra", "Kerala", 
"Rajasthan", "Delhi", "Andhra Pradesh", "Uttar Pradesh", "Gujarat", 
"West Bengal", "Mizoram", "Uttarakhand", "Assam", "Haryana", 
"Himachal Pradesh", "Jharkhand", "Punjab"), id = c(35, 8, 36, 
25, 11, 12, 9, 21, 5, 17, 24, 22, 1, 30, 23, 26, 6, 19, 7, 20
), long = c(74.8692906760123, 76.1671602023197, 84.4299347735266, 
88.47355094813, 78.2889834734203, 76.1073683433136, 76.4080579710788, 
73.8499033398323, 77.1154800785184, 79.964340328073, 80.5663333555389, 
71.5737853588608, 87.9835046818375, 92.8318089515678, 79.2071970183086, 
92.8261810120493, 76.3401988564537, 77.2453583452855, 85.5641242711505, 
75.4154856775423), lat = c(33.7066861126216, 14.7103409600977, 
20.5129916361452, 27.5703696825569, 23.5382000019923, 19.4517685463546, 
10.4515827401328, 26.5845654103506, 28.6433836730517, 15.7549664734525, 
26.9232961978376, 22.6974841269408, 23.8143407874487, 23.3070171585463, 
30.1564981917953, 26.3553441447585, 29.198093425323, 31.92360060294, 
23.6561315040147, 30.8424285351448), Type = c("Union Territory", 
"State", "State", "State", "State", "State", "State", "State", 
"Union Territory", "State", "State", "State", "State", "State", 
"State", "State", "State", "State", "State", "State"), low_prestige = c(1000, 
836, 195, 1000, 188, 441, 441, 736, 370, 235, 0, 151, 82, 42, 
28, 0, 0, 0, 0, 0), high_prestige = c(0, 0, 0, 0, 53, 149, 212, 
264, 630, 765, 808, 849, 918, 958, 972, 1000, 1000, 1000, 1000, 
1000)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-20L))

我试过的代码:

map1<-ggplot(final.plot1, aes( x = long, y = lat, group=id)) +
  geom_polygon(aes(fill = low_prestige), color = "black")
map1
gstyhher

gstyhher1#

另一种选择是使用sf包,该包与ggplot一起用于绘制地理数据。以下是一个可重现的示例:

library(ggplot2)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE

your_sf <- st_as_sf(final.plot1, coords = c('long', 'lat'))
                  
ggplot(your_sf) +
  geom_sf(aes(color = low_prestige))

创建于2022年11月28日,使用reprex v2.0.2

相关问题