3

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.


UPDATE

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 ids 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.

enter image description here

henhesu
  • 756
  • 4
  • 9

2 Answers2

3

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

library(ggplot2)

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


UPDATE

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

enter image description here

henhesu
  • 756
  • 4
  • 9
stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thanks a lot! I've upvoted your answer, but haven't "accepted" it yet, as I noticed that my problem is actually a bit more complex than my initial reprex. Apologies for the vagueness. I've updated my question accordingly. Any ideas on how to solve my actual problem as well? – henhesu Mar 04 '22 at 14:24
  • 1
    @lks_swrx Not 100% sure about your desired result. But you could try by mapping `group` on the `group` aes, i..e `ggplot(df, aes(x = id, y = value, fill = group, group = group))`. – stefan Mar 04 '22 at 17:37
  • Perfect, adding `group = group` actually did the trick. Exactly what I wanted, many thanks! I added the solution to your answer so other users can see it more clearly. – henhesu Mar 07 '22 at 08:24
2

If you want triangles, think you'll probably need to do it manually using some wrangling and geom_polygon, something like:

library(ggplot2)

df <- tibble::tibble(x = c(LETTERS[1:6], LETTERS[1:5]),
                     y = c(paste0("V", 1:6), paste0("V", 1:5)),
                     group = c(rep("group_1", 6), rep("group_2", 5)))

df1    <- df[!duplicated(interaction(df$x, df$y)),]
df2    <- df[duplicated(interaction(df$x, df$y)),]
df2    <- df[rep(seq(nrow(df)), each = 3),]
df2$x1 <- as.numeric(as.factor(df2$x))
df2$y1 <- as.numeric(as.factor(df2$y))
df2$x1 <- df2$x1 + c(-0.5, 0.5, 0.5)
df2$y1 <- df2$y1 + c(-0.5, -0.5, 0.5)
df2$z  <- rep(seq(nrow(df2)/3), each = 3)

ggplot(df1, aes(x = x, y = y, fill = group)) + 
  geom_tile() +
  geom_polygon(data = df2, aes(x = x1, y = y1, group = z))

Created on 2022-02-16 by the reprex package (v2.0.1)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87