重塑 table 用于图块图
Reshape table for tile plot
我有一个数据框,看起来像这样(我简化了它,实际上它很长)。
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 的 absence/presence。然而,我有很多旅行要在 R 上重塑这个 table 甚至绘制它。所需的输出将是:
我想绘制它。
对此问题的任何帮助将不胜感激。
谢谢
我们可以使用 pivot_longer
重塑为 'long' 格式,然后执行 count
并将其重塑回 'wide'
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 R
中的 table
+(table(unlist(data), rep(names(data), each = nrow(data))) != 0)
这是使用 tidyverse
中的 tidyr::pivot_longer
和 ggplot
的方法。
library(tidyverse)
data %>%
pivot_longer(cols = -miRs, names_to = "species", values_to = "listed_miRs") %>%
ggplot(aes(species, listed_miRs)) +
geom_tile()
我有一个数据框,看起来像这样(我简化了它,实际上它很长)。
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 的 absence/presence。然而,我有很多旅行要在 R 上重塑这个 table 甚至绘制它。所需的输出将是:
我想绘制它。 对此问题的任何帮助将不胜感激。 谢谢
我们可以使用 pivot_longer
重塑为 'long' 格式,然后执行 count
并将其重塑回 'wide'
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 R
table
+(table(unlist(data), rep(names(data), each = nrow(data))) != 0)
这是使用 tidyverse
中的 tidyr::pivot_longer
和 ggplot
的方法。
library(tidyverse)
data %>%
pivot_longer(cols = -miRs, names_to = "species", values_to = "listed_miRs") %>%
ggplot(aes(species, listed_miRs)) +
geom_tile()