2

我堆叠了一个数据框,显示values peridgroups:

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

我想创建一个热图,显示跨s(填充)value的每个(y)的每个(x)的“可用性”:idgroup

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

在此处输入图像描述

问题在于fill重叠:我只能看到 F/V6 仅在group_1(而不是group_2)中。但是,对于 ID A 到 E,值 V1 到 V5 在两个组中都可用,因此 的颜色group_2位于 顶部group_1,看起来它们仅在 中可用group_2

如果我使用facet_wrap(),可用性会更明显:

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

在此处输入图像描述

但是,在我的实际设置中,热图非常大,因此很难比较哪个值在哪个组中可用。

如果值在两组中都可用,是否可以将每个图块分成两半,如果它只存在于一组中,是否可以保持完整?所以在上面的第一个图中,蓝色瓷砖将被分成两半(同时显示蓝色和红色),红色瓷砖将保持原样。


更新

感谢 stefan 对使用position = "dodge". 但是,我注意到我的问题实际上比我上面的 reprex 复杂一点:每个都value可能出现在多个ids pergroup中。使用时position = "dodge",ggplot2 然后将每个“列”“划分”为与此中id出现的每个“列”一样多的value部分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")

您可以看到,在“A 列”中,三个图块被放置在彼此上方和相邻的位置,将可用空间分成三部分。我想要实现的是在“A列”中将这三对图块绘制在彼此的顶部,以便它们对齐,使用分配给每个值的“A列”的整个可用空间。

在此处输入图像描述

4

2 回答 2

3

一种选择是使用position="dodge"

library(ggplot2)

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

于 2022-02-16T18:57:52.153 回答
1

如果你想要三角形,认为你可能需要使用一些争吵和手动完成geom_polygon,例如:

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

reprex 包于 2022-02-16 创建(v2.0.1)

于 2022-02-16T19:38:01.290 回答