我正在使用该scatterpie
包在地图上生成饼图。但是,3 个饼图段 E、S、AC 将根据它们在每个位置的值具有不同的颜色。我不熟悉 scatterpie,但我似乎找不到执行此操作的最佳方法。我在 scatterpie 代码的填充部分中使用了 cut 函数,然后使用下面的 scale_fill_manual 根据此示例链接分配颜色。无论如何,这里是我喜欢做的事情的副本。我希望有人可以提供见解。
library(ggplot2)
library(scatterpie)
world_map <- map_data("world")
ggplot(world_map, aes(x = long, y = lat, group = group)) +
geom_polygon(fill="lightgray", colour = "white")
# Med Contries
med.countries <- c("Portugal", "Spain", "Albania", "Algeria", "Bosnia and Herzegovina", "Croatia", "Cyprus", "Egypt", "France", "Greece", "Israel","Italy",
"Lebanon","Libya", "Malta", "Monaco", "Montenegro","Morocco", "Slovenia", "Spain", "Syria", "Tunisia","Turkey")
map_data = data.frame(MPA=c("A","B","C","E","F","G"), long=c(3.1, 9.1, 20.5, 13.7, 9.7, 5.2), lat=c(42.19, 44.3, 37.4,44.90, 40.85, 43.12), E= c(0.2,0.4,0.3,0.6,0.1,0.7), S=c(0.3, 0.28, 0.5,0.3,0.3,0.15), AC=c(0.5,0.32, 0.2, 0.1, 0.6, 0.15))
med.maps <- map_data("world", region = med.countries)
colours_group = c("olivedrab4", "#0073C2FF", "#EFC000FF", "darkorange2", "#CD534CFF")
ggplot(map_data, aes(x = long, y = lat))+
geom_polygon(data = med.maps, aes(group = group),fill="lightgray", colour = "white")+
geom_scatterpie(data = map_data, aes(x= long, y= lat, group=MPA, fill= cut(c(map_data$E, map_data$S, map_data$AC), c(0,0.2,0.4,0.6,0.8))),
cols = c("E", "S", "AC"), pie_scale = 2) +
scale_fill_manual("vunrisk", values = colours_group)+
scale_y_continuous(limits=c(35,48))+
scale_x_continuous(limits=c(0,25))+
coord_equal() +
labs(title = "Piechart") +
theme_void()+
theme(legend.position = "right")