瓷砖图的整形表

问题描述 投票:0回答:2

我有一个看起来像这样的数据框(我简化了它,实际上它很长)。

enter image description here

data <- structure(list(miRs = structure(c(10L, 11L, 12L, 3L, 4L, 5L, 
6L, 1L, 2L, 7L, 9L, 8L), .Label = c("bantam", "miR-1", "miR-184|example1", 
"miR-184|example2", "miR-184|example3", "miR-184|example4", "miR-3", 
"miR-7", "miR-9", "miR-92|example1", "miR-92|example2", "miR-92|example3"
), class = "factor"), Apis.mellifera = structure(c(8L, 9L, 10L, 
4L, 5L, 6L, 7L, 2L, 3L, 1L, 1L, 1L), .Label = c("", "bantam", 
"miR-1", "miR-184|example1", "miR-184|example2", "miR-184|example3", 
"miR-184|example4", "miR-92|example1", "miR-92|example2", "miR-92|example3"
), class = "factor"), B..morix = structure(c(9L, 10L, 5L, 6L, 
7L, 8L, 2L, 3L, 4L, 1L, 1L, 1L), .Label = c("", "bantam", "miR-1", 
"miR-10", "miR-184|example1", "miR-184|example2", "miR-184|example3", 
"miR-184|example4", "miR-92|example1", "miR-92|example2"), class = "factor"), 
    D..mel = structure(c(8L, 9L, 10L, 4L, 5L, 6L, 7L, 2L, 3L, 
    1L, 1L, 1L), .Label = c("", "bantam", "miR-1", "miR-184|example1", 
    "miR-184|example2", "miR-184|example3", "miR-184|example4", 
    "miR-92|example1", "miR-92|example2", "miR-92|example3"), class = "factor"), 
    N..vitripennis = structure(c(8L, 9L, 10L, 4L, 5L, 6L, 7L, 
    2L, 3L, 1L, 1L, 1L), .Label = c("", "bantam", "miR-1", "miR-184|example1", 
    "miR-184|example2", "miR-184|example3", "miR-184|example4", 
    "miR-92|example1", "miR-92|example2", "miR-92|example3"), class = "factor"), 
    P..tepidariorum = structure(c(9L, 10L, 5L, 6L, 7L, 8L, 2L, 
    3L, 4L, 1L, 1L, 1L), .Label = c("", "bantam", "miR-1", "miR-10", 
    "miR-184|example1", "miR-184|example2", "miR-184|example3", 
    "miR-184|example4", "miR-92|example1", "miR-92|example2"), class = "factor"), 
    T..castaneum = structure(c(8L, 9L, 10L, 6L, 7L, 4L, 5L, 2L, 
    3L, 1L, 1L, 1L), .Label = c("", "bantam|LQNS02278082.1_33125", 
    "miR-1", "miR-184|LQNS02000211.1_1795", "miR-184|LQNS02000211.1_1950", 
    "miR-184|LQNS02000211.1_1952", "miR-184|LQNS02000211.1_1954", 
    "miR-92|example1", "miR-92|example2", "miR-92|example3"), class = "factor"), 
    S..maritima = structure(c(2L, 4L, 3L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L), .Label = c("", "miR-3", "miR-7", "miR-9"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-12L))

理想情况下,我想绘制一个图块,其中仅显示每个miR的缺失/存在。但是,我要花很多时间才能在R上重整此表或什至绘制它。所需的输出将是:enter image description here

我想作图。任何有关此问题的帮助将不胜感激。谢谢

r ggplot2 reshape
2个回答
0
投票

我们可以用pivot_longer整形为'long'格式,然后执行count并将其整形为'wide']

library(dplyr)
library(tidyr)
data %>% 
  type.convert(as.is = TRUE) %>%
  pivot_longer(everything()) %>% 
  filter(value != "") %>% 
  count(name, value) %>%
  pivot_wider(names_from = name, values_from = n, values_fill = list(n = 0))

或使用table中的base R

+(table(unlist(data), rep(names(data), each = nrow(data))) != 0)

0
投票

这里是使用tidyr::pivot_longer中的ggplottidyverse的方法。

library(tidyverse)
data %>% 
  pivot_longer(cols = -miRs, names_to = "species", values_to = "listed_miRs") %>%
  ggplot(aes(species, listed_miRs)) +
  geom_tile() 

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.