【问题标题】:Reshape table for tile plot为平铺图重塑表格
【发布时间】:2020-01-22 18:59:15
【问题描述】:

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

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 上重塑这张桌子,甚至绘制它。所需的输出将是:

我想绘制它。 对此问题的任何帮助将不胜感激。 谢谢

【问题讨论】:

    标签: r dataframe ggplot2 reshape


    【解决方案1】:

    这是使用tidyverse 中的tidyr::pivot_longerggplot 的方法。

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

    【讨论】:

    • 谢谢,我也得到了这个结果:a = melt(data = c, id.vars = "miRs") p
    • 你知道有什么方法可以将结果聚集在 ggplot 中以避免出现漏洞吗?谢谢
    • 我不确定什么手段对您的数据有意义,但您可以先查看forcats 包,尤其是forcats::fct_reorderforcats::fct_infreq
    【解决方案2】:

    我们可以使用pivot_longer 重新整形为“长”格式,然后执行count 并将其重新整形为“宽”

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

    或者使用来自base Rtable

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多