2
votes

I have stacked a data frame which shows values per id across groups:

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 groups (fill):

ggplot(df, aes(x = id, y = value, fill = group)) + 
  geom_tile()

enter image description here

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")

enter image description here

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.

1

1 Answers

1
votes

One option would be to use position="dodge":

library(ggplot2)

ggplot(df, aes(x = id, y = value, fill = group)) + 
  geom_tile(position = "dodge")