[英]NA values in choropleth plot legend with ggplot2 in R
在帶有 ggplot2 的等值線圖形 map 中,對應於變量Cuantil Esfuerzo Social por habitante
的 NA 值的元素自動包含在圖例中
我嘗試了幾件事:
scale_fill_manual()
中使用na.translate = F
會隱藏圖例中的“NA”項以及與這些值對應的多邊形。 所以 map 是不完整的。根據需要構建圖表有兩種選擇:
annotate()
),帶有一個矩形和文本。key
和label
的不同圖例分隔(添加空格)到“NA”值。數據: https://www.dropbox.com/s/b39pp4qmbhns658/geosmunicipiosmu.R?dl=0
代碼:
library(ggplot2)
geosmunicipiosmu <- dget("geosmunicipiosmu.R")
colors = c("#fee5d9","#fcae91","#fb6a4a","#de2d26")
mapaemu <- ggplot(geosmunicipiosmu) +
geom_sf(
aes(
fill = `Cuantil Esfuerzo Social por habitante`
),
#fill="white",
#fill=NA,
color="#FFFFFF",
size=0.5
) +
coord_sf(
xlim = c(-3.1, -0.5)
) +
theme_void() +
scale_fill_manual(
values = c(colors,"#2c2c2c"), # Color adicional para los valores "NA"
labels = c(
paste("[1Q]\n", format(cuartilin[2], big.mark=".", decimal.mark=",", nsmall = 2, digits = 2), "€", sep=""),
paste("(2Q]\n", format(cuartilin[3], big.mark=".", decimal.mark=",", nsmall = 2, digits = 2), "€", sep=""),
paste("(3Q]\n", format(cuartilin[4], big.mark=".", decimal.mark=",", nsmall = 2, digits = 2), "€", sep=""),
paste("(4Q]\n", format(cuartilin[5], big.mark=".", decimal.mark=",", nsmall = 2, digits = 2), "€", sep=""),
"Sin\ndatos" # Etiqueta personalizada para valores "NA"
),
guide = guide_legend(
direction = "horizontal",
nrow = 1,
label.position = "top",
label.hjust = 1,
keyheight = 0.75
)
) +
labs(
title = "Título",
subtitle = "Subtítulo",
fill = "" # Etiqueta para la Leyenda
) +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#ffffff", color = NA),
panel.background = element_rect(fill = "#ffffff", color = NA),
legend.background = element_rect(fill = "#ffffff", color = NA),
plot.caption.position = "plot",
legend.position = "bottom",
legend.text = element_text(size = 14)
)
mapaemu
map 現在:
我建議另一種選擇,包括在所需的NA
顏色上繪制一個基礎層,然后使用na.translate = F
方法將其與第二個彩色層結合起來。 通過這樣做, plot 將所有多邊形作為基礎層,並且圖例將省略NA
。 請參閱下面的代碼進行這些小的修改。
library(ggplot2)
geosmunicipiosmu <- dget("geosmunicipiosmu.R")
# Create cuartilin. This was not on your code, so I figured it out <--------------
cuartilin <- quantile(geosmunicipiosmu$`Esfuerzo Social por Habitante`, na.rm = TRUE)
colors <- c("#fee5d9", "#fcae91", "#fb6a4a", "#de2d26")
mapaemu <- ggplot(geosmunicipiosmu) +
# Add this line to create the base layer <--------------
geom_sf(fill = "grey50", color = "#FFFFFF") +
geom_sf(
aes(
fill = `Cuantil Esfuerzo Social por habitante`
),
color = "#FFFFFF",
size = 0.5
) +
coord_sf(
xlim = c(-3.1, -0.5)
) +
theme_void() +
scale_fill_manual(
# Now remove NA from legend <--------------
na.translate = FALSE,
values = colors, # No NA color needed any more <--------------
labels = c(
paste("[1Q]\n", format(cuartilin[2], big.mark = ".", decimal.mark = ",", nsmall = 2, digits = 2), "€", sep = ""),
paste("(2Q]\n", format(cuartilin[3], big.mark = ".", decimal.mark = ",", nsmall = 2, digits = 2), "€", sep = ""),
paste("(3Q]\n", format(cuartilin[4], big.mark = ".", decimal.mark = ",", nsmall = 2, digits = 2), "€", sep = ""),
paste("(4Q]\n", format(cuartilin[5], big.mark = ".", decimal.mark = ",", nsmall = 2, digits = 2), "€", sep = "")
), # No NA label needed any more <--------------
guide = guide_legend(
direction = "horizontal",
nrow = 1,
label.position = "top",
label.hjust = 1,
keyheight = 0.75
)
) +
labs(
title = "Título",
subtitle = "Subtítulo",
fill = "" # Etiqueta para la Leyenda
) +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#ffffff", color = NA),
panel.background = element_rect(fill = "#ffffff", color = NA),
legend.background = element_rect(fill = "#ffffff", color = NA),
plot.caption.position = "plot",
legend.position = "bottom",
legend.text = element_text(size = 14)
)
mapaemu
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.