Reputation: 57
I have data i want to visualise using geom_tile. The data deal with references in text data from either Germany or Spain to other European countries.
Example data:
data<- tribble(
~Ref_country, ~Country, ~Share,
"ENG", "DE", 0.27,
"ENG", "ES", 0.24,
"ESP", "DE", 0.21,
"ESP", "ES", NA,
"GER", "DE", NA,
"GER", "ES", 0.13,
"FRA", "DE", 0.11,
"FRA", "ES", 0.18,
"ITA", "DE", 0.1,
"ITA", "ES", 0.17,
"POR", "DE", 0.03,
"POR", "ES", 0.07,
"RUS", "DE", 0.02,
"RUS", "ES", 0.02,
"NLD", "DE", 0.04,
"NLD", "ES", 0.01,
"BEL", "DE", 0.02,
"BEL", "ES", 0.03,
"SWE", "DE", 0.01,
"SWE", "ES", 0.01
)
Since i do not count references from a country to itself, GER-DE and ESP-ES are NA.
I plot this with ggplot, geom_tile and scale_fill_distiller:
x %>%
ggplot(., aes(x = Ref_country, y = Country, fill = Share)) +
geom_tile(color = "white", linewidth = 0.1) +
scale_fill_distiller(
type = "seq",
direction = 1,
palette = "Greys",
na.value = "white"
)
My question is: is there a way to distinguish the NAs not by color, but by pattern? Maybe through a creative application of ggpattern. Or alternatively, is it possible to add text ("NA") or a shape on top of the respective NA tiles? Since I will likely publish it in black and white, color is not a good distinguishing property.
Thanks in advance.
PS: As another alternative, can the color scale be restricted to greys that are clearly distinguishible from white?
Upvotes: 0
Views: 773
Reputation: 1605
Alternative solution using ggpattern
library(ggplot2)
library(ggpattern)
data %>%
mutate(alpha = ifelse(is.na(Share), 1, 0)) %>%
ggplot(aes(x=Ref_country, y=Country, fill=Share)) +
ggpattern::geom_tile_pattern(aes(pattern_alpha=alpha, pattern_fill=Share, pattern_size=NA) +
scale_fill_distiller(
type = "seq",
direction = 1,
palette = "Greys",
na.value = "transparent",
guide = guide_bins(axis = FALSE, keywidth = unit(1, "cm"))
) +
ggpattern::scale_pattern_fill_distiller(
type = "seq",
direction = 1,
palette = "Greys",
na.value = "black",
guide = guide_bins(axis = FALSE, keywidth = unit(1, "cm"))
) +
labs(fill="Share", pattern_fill="Share", x="Ref_country", y="Country")
theme(legend.position = "bottom")
This should give something like (although this below is applied to a bar chart):
Upvotes: 0
Reputation: 125897
As you mentioned in your comment, the first option which came to my mind would be to use a "transparent"
color for the missings. Additionally I switched to guide_bins
for the legend. And of course could you also add a symbol for the missings using e.g. a geom_text
layer or perhaps a geom_point
:
library(ggplot2)
library(magrittr)
data %>%
ggplot(., aes(x = Ref_country, y = Country, fill = Share)) +
geom_tile(color = "white", linewidth = 0.1) +
scale_fill_distiller(
type = "seq",
direction = 1,
palette = "Greys",
na.value = "transparent",
guide = guide_bins(axis = FALSE, keywidth = unit(1, "cm"))
) +
theme(legend.position = "bottom")
data %>%
ggplot(., aes(x = Ref_country, y = Country, fill = Share)) +
geom_tile(color = "white", linewidth = 0.1) +
geom_text(data = ~ subset(.x, is.na(Share)), aes(label = "X")) +
scale_fill_distiller(
type = "seq",
direction = 1,
palette = "Greys",
na.value = "transparent",
guide = guide_bins(axis = FALSE, keywidth = unit(1, "cm"))
) +
theme(legend.position = "bottom")
Upvotes: 2