简体   繁体   English

将自定义图例添加到 R 栅格 spplot map

[英]Adding a customized legend to a R raster spplot map

I would like to ask you for a few advices on a R cartography with Raster / spplot I am currently working on.我想就我目前正在研究的带有光栅/spplot 的 R 制图向您请教一些建议。 I am a novice so I apologize in advance should the methods I used to be not at all optimal!我是新手,所以如果我以前的方法根本不是最优的,我提前道歉!

=> So: I have a raster object and almost got what I wanted, but I have troubles with the legend and the result looks kind of childish. => 所以:我有一个光栅 object 几乎得到了我想要的,但是我对图例有问题,结果看起来有点幼稚。 I'd like to get something a bit more "professional".我想得到一些更“专业”的东西。 I'd like to 1) improve the overall aesthetics and 2) add legends on my plot such as this concentric bubble size legend proposed in this other post: create a concentric circle legend .我想 1)提高整体美感和 2)在我的 plot 上添加图例,例如在另一篇文章中提出的同心气泡大小图例: 创建同心圆图例

Here is what I have right now: death rate and exposure in France这是我现在所拥有的:法国的死亡率和暴露率

What I think might improve the map:我认为可能会改进 map:

    1. Use a concentric circles bubble legend for hospital volume and put it on the top right corner使用同心圆气泡图例作为医院体积并将其放在右上角
    1. Add transparency to my points.增加我的观点的透明度。 Here I have 13 bubbles, but the real map has about 600 with many overlapping (especially in Paris area).这里我有 13 个气泡,但真正的 map 有大约 600 个,并且有很多重叠(尤其是在巴黎地区)。
    1. Add a legend to my colour gradient为我的颜色渐变添加图例
    1. If you have any tips / comments do not hesitate: I'm a beginner but eager to learn :)如果您有任何提示/意见,请不要犹豫:我是初学者,但渴望学习 :)

I've enclosed a simplified full code (13 hospitals instead of 600, data completely edited, variable names changed... So no need to interprete.).我附上了一个简化的完整代码(13 家医院而不是 600 家,数据完全编辑,变量名称更改......所以不需要解释。)。 I've edited it so that you can just copy / paste easily.我已经对其进行了编辑,以便您可以轻松复制/粘贴。

####################################################################
####################################################################
# 1) DATA PREPARATION


# Packages
library(raster)
library(rgeos)
library(latticeExtra)
library(sf)

# Mortality dataset
french_regions=c("IDF", "NE", "NO", "SE", "SO")
death_rates_reg=c(0.032,0.014,0.019,0.018,0.021)
region_mortality=data.frame(french_regions,death_rates_reg)

# Hospital dataset
hospital_id=1:13
expo=c(0.11,0.20,0.17,0.25,0.18,0.05,0.07,0.25,0.40,0.70,0.45,0.14,0.80)
volume=sample(1:200, 13, replace=TRUE)
lat=c(44.8236,48.8197,45.7599,45.2785,48.9183,50.61,43.6356,47.9877,48.8303,48.8302,48.8991,43.2915,48.7232)
long=c(-0.57979,7.78697,4.79666,6.3421,2.52365,3.03763,3.8914,-4.095,2.34038,2.31117,2.33083,5.56335,2.45025)
french_hospitals=data.frame(hospital_id,expo,volume,lat,long)

# French regions map object - merge of departments according to phone codes
formes <- getData(name="GADM", country="FRA", level=2)
formes$NAME_3=0 # NAME_3 = new mega-regions IDF, NE, NO, SE, SO

formes$NAME_3[formes$NAME_1=="Auvergne-Rhône-Alpes"]="SE"
formes$NAME_3[formes$NAME_1=="Bourgogne-Franche-Comté"]="NE"
formes$NAME_3[formes$NAME_1=="Bretagne"]="NO"
formes$NAME_3[formes$NAME_1=="Centre-Val de Loire"]="NO"
formes$NAME_3[formes$NAME_1=="Corse"]="SE"
formes$NAME_3[formes$NAME_1=="Grand Est"]="NE"
formes$NAME_3[formes$NAME_1=="Hauts-de-France"]="NE"
formes$NAME_3[formes$NAME_1=="Île-de-France"]="IDF"
formes$NAME_3[formes$NAME_1=="Normandie"]="NO"
formes$NAME_3[formes$NAME_1=="Nouvelle-Aquitaine"]="SO"
formes$NAME_3[formes$NAME_1=="Occitanie"]="SO"
formes$NAME_3[formes$NAME_1=="Pays de la Loire"]="NO"
formes$NAME_3[formes$NAME_1=="Provence-Alpes-Côte d'Azur"]="SE"
formes$NAME_3[formes$NAME_2=="Aude"]="SE"
formes$NAME_3[formes$NAME_2=="Gard"]="SE"
formes$NAME_3[formes$NAME_2=="Hérault"]="SE"
formes$NAME_3[formes$NAME_2=="Lozère"]="SE"
formes$NAME_3[formes$NAME_2=="Pyrénées-Orientales"]="SE"
groups = aggregate(formes, by = "NAME_3")

# Colour palettes
couleurs_death=colorRampPalette(c('gray100','gray50'))
couleurs_expo=colorRampPalette(c('green','gold','red','darkred'))

# Hospitals bubble sizes and colours

my_colours=couleurs_expo(401)
french_hospitals$bubble_color="Initialisation"
french_hospitals$indice=round(french_hospitals$expo*400,digits=0)+1
french_hospitals$bubble_size=french_hospitals$volume*(1.5/50)

for(i in 1:length(french_hospitals$bubble_color)){
  french_hospitals$bubble_color[i]=my_colours[french_hospitals$indice[i]]
  
}


####################################################################
####################################################################
# 2) MAP

# Assignation of death rates to regions
idx <- match(groups$NAME_3, region_mortality$french_regions)
concordance <- region_mortality[idx, "death_rates_reg"]
groups$outcome_char <- concordance

# First map: region colours = death rates
graphA=spplot(groups, "outcome_char", col.regions=couleurs_death(500),
              par.settings = list(fontsize = list(text = 12)),
              main=list(label=" ",cex=1),colorkey = list(space = "bottom", height = 0.85))

# Second map: hospital bubbles = exposure
GraphB=graphA + layer(panel.points(french_hospitals[,c(5,4)],col=french_hospitals$bubble_color,pch=20, cex=french_hospitals$bubble_size))


# Addition of the legend

Bubble_location=matrix(data=c(-4.0,-2.0,0.0,-4.0,-2.0,0.0,42.3,42.3,42.3,41.55,41.55,41.55),nrow=6,ncol=2)
GraphC1=GraphB + layer(panel.points(Bubble_location, col=c(my_colours[5],my_colours[125],my_colours[245],"black","black","black"), pch=19,cex=c(2.5,2.5,2.5,5.0,2.0,1.0)))
Bubble_location2=matrix(data=c(-3.4,-1.27,0.55, -3.65, -3.3 , -3.4,-1.52,0.48,42.31,42.31,42.31,42.55,41.9, 41.56,41.56,41.56),nrow=8,ncol=2)
GraphC2=GraphC1+layer(panel.text(Bubble_location2, label=c("0%","30%","60%", "Exposure:", "Hospital volume:", "125","50","25"), col="black", cex=1.0))

# Final map
GraphC2

Thank you in advance for your help, (I know this is a lot, do not feel forced to dive in the code)预先感谢您的帮助,(我知道这很多,不要觉得被迫潜入代码)

It isn't pretty, but I think this can get you started baring a more complete answer from someone else.这并不漂亮,但我认为这可以让你开始从其他人那里获得更完整的答案。 I'd suggest using ggplot instead of spplot.我建议使用 ggplot 而不是 spplot。 The only thing you need to do is convert your sp object to sf to integrate with ggplot.您唯一需要做的就是将您的 sp object 转换为 sf 以与 ggplot 集成。 The bubble plot needs a lot of guess and check, so I'll leave that up to you...气泡 plot 需要大量猜测和检查,所以我将把它留给你......

Map layout design is still better in GIS software, in my opinion.在我看来,Map 布局设计在 GIS 软件中仍然更好。

library(sf)
library(ggplot2)

# Convert sp to sf
groups_sf <- st_as_sf(groups)
# Make reference dataframe for concentric bubble legend
bubble_legend <- data.frame(x = c(8.5, 8.5, 8.5), y = c(50, 50, 50), size = c(3, 6, 9))

ggplot() +
  geom_sf(data = groups_sf) +
  geom_point(data = french_hospitals, aes(x = long, y = lat, color = indice, size = bubble_size), alpha = 0.7) +
  geom_point(data = bubble_legend, aes(x = x, y = y + size/50), size = bubble_legend$size, shape = 21, color = "black", fill = NA) +
  geom_text(data = bubble_legend, aes(x = x + 0.5, y = y + size/50, label = size), size = 3) +
  scale_color_gradient(low = "green", high = "red") +
  guides(size="none")

在此处输入图像描述

Let me know what you think.让我知道你的想法。 I can help troubleshoot more if there are any issues.如果有任何问题,我可以帮助解决更多问题。

Thank you for your answer Skaqqs, very appreciated.谢谢你的回答Skaqqs,非常感谢。 This is in my opinion a good step forward,.在我看来,这是向前迈出的一大步。 I tried it quickly on the real data and it already looks way better.我在真实数据上快速尝试了它,它看起来已经好多了。 especially with the transparency.尤其是透明度。 I can't really show more since that's sensitive data on a trendy topic and we want to keep it confidential as much as possible until article submission.我真的不能展示更多,因为这是一个流行话题的敏感数据,我们希望在文章提交之前尽可能保密。

I'll move on from this good starting base and update you.我将从这个良好的起点继续前进并更新您。

Thank you:)谢谢:)

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

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