我用geom_tile创建了一个矩阵曲线图,但是看到行有多个值在曲线图中重叠.有没有办法用代表该条目的多种 colored颜色 填充每个瓷砖?可能类似于此(理想情况下,形状应该是矩形而不是三角形):split fill in ggplot geom_tile (or heatmap): two colors by third value

enter image description here

代码:

# Create color palette

par(mar=c(0,0,1,0))

coul <- brewer.pal(9, "Set3") 

ggplot(plot_data, aes(x = from, y = to, fill = agreement_num)) +
  geom_tile() +
  scale_fill_manual(values = coul) +
  theme_bw() +
  scale_x_discrete(drop = FALSE) +
  scale_y_discrete(drop = FALSE) +
  theme(
    axis.text.x = element_text(size = 9,angle = 270, hjust = 0,vjust=0),
    axis.text.y = element_text(size = 9),
    aspect.ratio = 1)

可复制的示例:

plot_data <- structure(list(from = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, NA, NA, NA, NA), levels = c("Afghanistan", 
"Kazakhstan", "Kyrgyzstan", "Tajikistan", "Turkmenistan", "Uzbekistan"
), class = "factor"), to = structure(c(1L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 2L, 6L, NA, NA, NA, NA), levels = c("Afghanistan", 
"Kazakhstan", "Kyrgyzstan", "Tajikistan", "Turkmenistan", "Uzbekistan"
), class = "factor"), weight = c(1291072130433.34, 480160896152.234, 
480160896152.234, 480160896152.234, 480160896152.234, 480160896152.234, 
480160896152.234, 3474907531417.02, 3474907531417.02, 3474907531417.02, 
867103764128.709, 867103764128.709, 7791981051421.92, 7791981051421.92, 
133799551.098735, 1102379004.66647, NA, NA, NA, NA), agreement_num = structure(c(NA, 
1L, 5L, 2L, 6L, 3L, 4L, 2L, 1L, 6L, 1L, 6L, 1L, 6L, NA, NA, NA, 
NA, NA, NA), levels = c("51", "58", "133", "135", "176", "224"
), class = "factor")), row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
"NA", "NA.1", "NA.2", "NA.3"), class = "data.frame")

推荐答案

一种 Select 是使用geom_rect和一些手动计算来绘制每个瓷砖的多个矩形:

library(ggplot2)
library(dplyr, warn = FALSE)

plot_data <- plot_data |>
  mutate(
    x = as.numeric(from),
    y = as.numeric(to),
    ymin = y - .5, ymax = y + .5
  ) |>
  mutate(
    n = n(),
    xmin = x + scales::rescale(row_number(),
      from = c(1, unique(n) + 1),
      to = .5 * c(-1, 1)
    ),
    xmax = x + scales::rescale(row_number() + 1,
      from = c(1, unique(n) + 1),
      to = .5 * c(-1, 1)
    ),
    .by = c(from, to)
  )

coul <- RColorBrewer::brewer.pal(9, "Set3") 

ggplot(plot_data, aes(x = from, y = to, fill = agreement_num)) +
  geom_rect(
    aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)
  ) +
  scale_fill_manual(values = coul) +
  theme_bw() +
  scale_x_discrete(drop = FALSE) +
  scale_y_discrete(drop = FALSE) +
  theme(
    axis.text.x = element_text(
      size = 9, angle = 270,
      hjust = 0, vjust = 0
    ),
    axis.text.y = element_text(size = 9),
    aspect.ratio = 1
  )
#> Warning: Removed 4 rows containing missing values (`geom_rect()`).

R相关问答推荐

R:连接值,而不是变量?

在发布到PowerBI Service时,是否可以使用R脚本作为PowerBI的数据源?

R中的子集文件—读取文件名索引为4位数字序列,例如0001到4000,而不是1到4000)

derrr mutate case_when grepl不能在R中正确返回值

如何将旋转后的NetCDF转换回正常的纬度/经度网格,并使用R?

当月份额减go 当月份额

如何在R中对深度嵌套的tibbles中的非空连续行求和?

多个模拟序列间的一种预测回归关系

通过在colname中查找其相应值来创建列

如何删除最后一个可操作对象

将Posict转换为数字时的负时间(以秒为单位)

在数据帧列表上绘制GGPUP

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

将工作目录子文件夹中的文件批量重命名为顺序

如何根据未知数的多列排除重复行

有没有办法通过str_Detect()或其他字符串匹配函数来连接两个长度不等的数据帧?

使用同一行中的前一个值填充R矩阵中的缺失值

在鼠标悬停时使用Plotly更改geom_point大小

如何在R中的两列以上使用联合(&U)?

具有某些列的唯一值的数据帧