I have stacked a data frame which shows value
s per id
across group
s:
df <- tibble::tibble(id = c(LETTERS[1:6], LETTERS[1:5]),
value = c(paste0("V", 1:6), paste0("V", 1:5)),
group = c(rep("group_1", 6), rep("group_2", 5)))
df
#> # A tibble: 11 x 3
#> id value group
#> <chr> <chr> <chr>
#> 1 A V1 group_1
#> 2 B V2 group_1
#> 3 C V3 group_1
#> 4 D V4 group_1
#> 5 E V5 group_1
#> 6 F V6 group_1
#> 7 A V1 group_2
#> 8 B V2 group_2
#> 9 C V3 group_2
#> 10 D V4 group_2
#> 11 E V5 group_2
I want to create a heatmap showing the "availability" of each value
(x) for each id
(y) across group
s (fill):
ggplot(df, aes(x = id, y = value, fill = group)) +
geom_tile()
The problem is that the fill
overlaps: All I can see is that F/V6 is only in group_1
(and not in group_2
). However, for IDs A to E, values V1 to V5 are available in both groups, and thus the color of group_2
is on top of group_1
, making it look like they are only available in group_2
.
If I use facet_wrap()
, the availability is more obvious:
ggplot(df, aes(x = id, y = value, fill = group)) +
geom_tile() +
facet_wrap("group")
However, in my real setting, the heatmap is very large so it is difficult to compare which values are available in which group.
Is it possible to split each tile in half if the value is available in both groups and keep it full if it is only present in one group? So in the first plot above, the blue tiles would be split in half (showing both blue and red), and the red tile would remain as is.
Thanks for stefan's excellent hint on using position = "dodge"
. However, I noticed that my problem is actually a bit more complex than my reprex above: Each value
may appear in multiple id
s per group
. When using position = "dodge"
, ggplot2 then "divides" each id
"column" in as many parts as there are occurrences of each value
within this id
:
df <- tibble::tibble(id = c("A", "A", "A", "B", "B", "C", "C", "C", "A", "A", "B", "B", "C", "C"),
value = c("V1", "V2", "V3", "V1", "V3", "V1", "V2", "V4", "V1", "V2", "V1", "V3", "V1", "V4"),
group = c(rep("group_1", 8), rep("group_2", 6)))
df
#> # A tibble: 14 x 3
#> id value group
#> <chr> <chr> <chr>
#> 1 A V1 group_1
#> 2 A V2 group_1
#> 3 A V3 group_1
#> 4 B V1 group_1
#> 5 B V3 group_1
#> 6 C V1 group_1
#> 7 C V2 group_1
#> 8 C V4 group_1
#> 9 A V1 group_2
#> 10 A V2 group_2
#> 11 B V1 group_2
#> 12 B V3 group_2
#> 13 C V1 group_2
#> 14 C V4 group_2
ggplot(df, aes(x = id, y = value, fill = group)) +
geom_tile(position = "dodge")
You can see that in "column A" the three tiles are placed both above and next to each other, splitting the available space in three. What I want to achieve is plotting these three pairs of tiles in "column A" on top of each other so they are aligned, using the whole available space alloted to "column A" for each value.
One option would be to use position="dodge"
:
library(ggplot2)
ggplot(df, aes(x = id, y = value, fill = group)) +
geom_tile(position = "dodge")
You could try by mapping group on the group
aes:
ggplot(df, aes(x = id, y = value, fill = group, group = group)) +
geom_tile(position = "dodge", color = "black") # adding 'color' for borders