I want to add no data color as grey to the fill part in the map.
ggplot(GDP_XXI_average_coor,aes(long,lat,group=group))+
geom_polygon(aes(fill=Average_GDP),colour="white")+
scale_fill_viridis_b(breaks=c(0, 1000, 2000, 5000, 10000, 20000, 50000),
na.value="grey")+
labs(fill="GDP per \ncapita ",
title="GDP per capita in 21st century", na.value= "No data") + theme_map()
Used packages:library(ggplot2) library(ggmap) library(ggthemes)
I tried to use na.value="grey" but it doesn't appear in the fill. I'm showing my map too.
dput(head(GDP_XXI_average_coor,20))
structure(list(long = c(-69.8991241455078, -69.8957061767578,
-69.9421920776367, -70.004150390625, -70.0661163330078, -70.0508804321289,
-70.0351104736328, -69.97314453125, -69.9118118286133, -69.8991241455078,
74.8913116455078, 74.8402328491211, 74.7673797607422, 74.7389602661133,
74.7266616821289, 74.6689453125, 74.5589904785156, 74.3721694946289,
74.3761672973633, 74.4979553222656), lat = c(12.4520015716553,
12.4229984283447, 12.4385251998901, 12.50048828125, 12.5469722747803,
12.5970697402954, 12.6141109466553, 12.567626953125, 12.48046875,
12.4520015716553, 37.2316398620605, 37.2250518798828, 37.2491722106934,
37.28564453125, 37.2907218933105, 37.2667007446289, 37.2366218566895,
37.15771484375, 37.1373519897461, 37.0572242736816), group = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), order = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L), region = c("Aruba", "Aruba", "Aruba",
"Aruba", "Aruba", "Aruba", "Aruba", "Aruba", "Aruba", "Aruba",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan"
), subregion = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), Average_GDP = c(28473.0717233333,
28473.0717233333, 28473.0717233333, 28473.0717233333, 28473.0717233333,
28473.0717233333, 28473.0717233333, 28473.0717233333, 28473.0717233333,
28473.0717233333, 471.741233426316, 471.741233426316, 471.741233426316,
471.741233426316, 471.741233426316, 471.741233426316, 471.741233426316,
471.741233426316, 471.741233426316, 471.741233426316)), row.names = c(NA,
20L), class = "data.frame")
Here's one possible solution which creates a new "alpha" legend, labels it, then overrides the display to make it look like "no data = grey":
library(ggplot2)
library(ggmap)
library(ggthemes)
library(gapminder)
library(dplyr)
world <- map_data("world")
world |> left_join(gapminder |>
filter(year == 2007),
by = c("region" = "country")) |>
ggplot(aes(long, lat, group = group, alpha = "No data")) +
geom_polygon(aes(fill = gdpPercap), colour = "white") +
scale_fill_viridis_b(breaks = c(0, 1000, 2000, 5000, 10000, 20000, 50000),
na.value = "grey") +
labs(fill = "GDP per \ncapita ",
title = "GDP per capita in 21st century",
na.value = "No data") + theme_map() +
scale_alpha_manual("", values = 1) +
guides(
fill = guide_colorsteps(order = 1),
alpha = guide_legend(order = 2, override.aes = list(fill = "grey"))
)