簡體   English   中英

plot 圖例中的 NA 值與 R 中的 ggplot2

[英]NA values ​in choropleth plot legend with ggplot2 in R

在帶有 ggplot2 的等值線圖形 map 中,對應於變量Cuantil Esfuerzo Social por habitante的 NA 值的元素自動包含在圖例中

我嘗試了幾件事:

  1. scale_fill_manual()中使用na.translate = F會隱藏圖例中的“NA”項以及與這些值對應的多邊形。 所以 map 是不完整的。
  2. 我使用的另一個選項(如代碼中所示)是為“NA”值定義顏色和特定的 label(Sin datos),以賦予圖表更多含義。

根據需要構建圖表有兩種選擇:

  1. 在圖例中隱藏“NA”值的 label。 並為“NA”值添加一個注釋( annotate() ),帶有一個矩形和文本。
  2. 為了能夠將具有四分位數間隔的圖例和僅使用相應的keylabel的不同圖例分隔(添加空格)到“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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM