叠加矩阵图
Overlaying matrix plots
在我的实验中,每个板有 96 个孔,每个孔随时间产生 5 种不同强度的颜色。我已经研究出如何针对每种颜色绘制时间矩阵。但是我不知道如何叠加所有不同的颜色。
这是一些示例数据。为了简单起见,我只包含了 4 个孔中的两种颜色。
data <- structure(list(well = c("A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03",
"A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03",
"A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03",
"A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03", "A04",
"A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04",
"A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04",
"A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04",
"A04"), channel = c("channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_5", "channel_5", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_5", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_5", "channel_5", "channel_1",
"channel_1", "channel_5", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_5", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_5", "channel_5", "channel_5", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_5", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5"
), time = c(1515L, 1761L, 1762L, 1763L, 1786L, 1787L, 1790L,
1791L, 1828L, 2282L, 2576L, 2601L, 2602L, 2626L, 2627L, 2668L,
2674L, 2678L, 2679L, 2689L, 2868L, 3162L, 3739L, 4326L, 4656L,
4686L, 4687L, 4688L, 4690L, 4691L, 4717L, 4718L, 4727L, 4910L,
5495L, 6080L, 6649L, 6918L, 7196L, 7738L, 8260L, 8763L, 9234L,
9681L, 10090L, 10466L, 10806L, 11106L, 11371L, 1513L, 1760L,
1761L, 1782L, 1783L, 1784L, 1785L, 1786L, 1787L, 1823L, 2567L,
2597L, 2622L, 2656L, 2657L, 2664L, 2665L, 2666L, 2667L, 2679L,
2680L, 2768L, 3147L, 3718L, 3988L, 4147L, 4298L, 4491L, 4524L,
4559L, 4591L, 4625L, 4654L, 4661L, 4678L, 4679L, 4680L, 4681L,
4682L, 4684L, 4685L, 4696L, 4724L, 4876L, 5454L, 6034L, 6598L,
6864L, 7141L, 7680L, 8201L, 8702L, 10030L, 10407L, 10751L, 11056L,
11326L, 1475L, 1722L, 1723L, 1743L, 1748L, 1783L, 2522L, 2553L,
2579L, 2612L, 2620L, 2622L, 2635L, 3099L, 3667L, 4244L, 4537L,
4570L, 4599L, 4631L, 4640L, 4819L, 5394L, 5970L, 6529L, 6793L,
7066L, 7598L, 8113L, 8607L, 10277L, 10610L, 10903L, 11162L, 1460L,
1704L, 1727L, 1728L, 1731L, 1767L, 2530L, 2531L, 2558L, 2591L,
2599L, 2600L, 2601L, 2614L, 4213L, 4504L, 4537L, 4573L, 4598L,
4607L, 4784L, 5355L, 5927L, 6482L, 6745L, 7016L, 7545L, 8056L,
10817L), rfu = c(20855L, 32767L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 14535L, 10201L, 11175L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 32767L, 32767L, 10556L, 10828L,
12400L, 14680L, 17187L, 32767L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 15759L, 15860L, 15713L, 15634L, 14951L,
14881L, 13628L, 13086L, 11895L, 10124L, 10167L, 10645L, 10960L,
12664L, 13541L, 12867L, 31668L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 19641L, 10447L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 32767L, 32767L, 32767L, 32767L,
13635L, 10272L, 11793L, 13124L, 11412L, 14429L, 13771L, 32095L,
29669L, 14818L, 28252L, 32767L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 32767L, 12187L, 15213L, 15434L,
15107L, 15317L, 14560L, 14562L, 13488L, 12820L, 11533L, 10083L,
10523L, 12379L, 13242L, 12629L, 23923L, 32767L, 32767L, 32767L,
32767L, 21650L, 10118L, 32767L, 32767L, 32767L, 32767L, 32767L,
32767L, 10049L, 11715L, 13765L, 11153L, 29499L, 32767L, 32767L,
32767L, 14637L, 14818L, 14733L, 14589L, 13889L, 13749L, 12802L,
12511L, 11508L, 10137L, 11727L, 12512L, 11989L, 21089L, 31649L,
32767L, 32767L, 32767L, 22163L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 11180L, 10074L, 26622L, 32739L,
32767L, 32767L, 12035L, 12157L, 12177L, 11955L, 11471L, 11481L,
10626L, 10215L, 10009L)), row.names = c(NA, -169L), class = "data.frame")
# Matrix plot of the first colour (channel_1)
ggplot(subset(data, channel=="channel_1"), aes(x=time, y=well)) +
geom_raster(aes(fill=rfu)) +
scale_fill_gradient(low="black", high="blue") +
dark_theme_bw()
# Matrix plot of the second colour (channel_5)
ggplot(subset(data, channel=="channel_5"), aes(x=time, y=well)) +
geom_raster(aes(fill=rfu)) +
scale_fill_gradient(low="black", high="orange") +
dark_theme_bw()
但是有没有可能把所有的颜色都放在同一个图上呢??
使用 ggnewscale
包,它允许多个比例和图例实现相同的审美,你可以这样做:
library(ggplot2)
library(ggnewscale)
library(ggdark)
data_split <- split(data, data$channel)
ggplot(mapping = aes(x = time, y = well)) +
geom_raster(data = data_split[[1]], aes(fill = rfu)) +
scale_fill_gradient(low = "black", high = "blue") +
new_scale_fill() +
geom_raster(data = data_split[[2]], aes(fill = rfu)) +
scale_fill_gradient(low = "black", high = "orange") +
dark_theme_bw()
或者您可以使用例如mapply
像这样为每个通道添加图层:
cols_high <- c("blue", "orange")
ggplot(mapping = aes(x = time, y = well)) +
mapply(function(df, color) {
list(
geom_raster(data = df, aes(fill = rfu)),
scale_fill_gradient(low = "black", high = color),
new_scale_fill()
)
}, data_split, cols_high) +
dark_theme_bw()
在我的实验中,每个板有 96 个孔,每个孔随时间产生 5 种不同强度的颜色。我已经研究出如何针对每种颜色绘制时间矩阵。但是我不知道如何叠加所有不同的颜色。
这是一些示例数据。为了简单起见,我只包含了 4 个孔中的两种颜色。
data <- structure(list(well = c("A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01", "A01",
"A01", "A01", "A01", "A01", "A01", "A01", "A01", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02", "A02",
"A02", "A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03",
"A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03",
"A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03",
"A03", "A03", "A03", "A03", "A03", "A03", "A03", "A03", "A04",
"A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04",
"A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04",
"A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04", "A04",
"A04"), channel = c("channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_5", "channel_5", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_5", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_5", "channel_5", "channel_1",
"channel_1", "channel_5", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_5", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_5", "channel_5", "channel_5", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_5", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_5", "channel_5", "channel_5", "channel_5",
"channel_5", "channel_5", "channel_5", "channel_5", "channel_5"
), time = c(1515L, 1761L, 1762L, 1763L, 1786L, 1787L, 1790L,
1791L, 1828L, 2282L, 2576L, 2601L, 2602L, 2626L, 2627L, 2668L,
2674L, 2678L, 2679L, 2689L, 2868L, 3162L, 3739L, 4326L, 4656L,
4686L, 4687L, 4688L, 4690L, 4691L, 4717L, 4718L, 4727L, 4910L,
5495L, 6080L, 6649L, 6918L, 7196L, 7738L, 8260L, 8763L, 9234L,
9681L, 10090L, 10466L, 10806L, 11106L, 11371L, 1513L, 1760L,
1761L, 1782L, 1783L, 1784L, 1785L, 1786L, 1787L, 1823L, 2567L,
2597L, 2622L, 2656L, 2657L, 2664L, 2665L, 2666L, 2667L, 2679L,
2680L, 2768L, 3147L, 3718L, 3988L, 4147L, 4298L, 4491L, 4524L,
4559L, 4591L, 4625L, 4654L, 4661L, 4678L, 4679L, 4680L, 4681L,
4682L, 4684L, 4685L, 4696L, 4724L, 4876L, 5454L, 6034L, 6598L,
6864L, 7141L, 7680L, 8201L, 8702L, 10030L, 10407L, 10751L, 11056L,
11326L, 1475L, 1722L, 1723L, 1743L, 1748L, 1783L, 2522L, 2553L,
2579L, 2612L, 2620L, 2622L, 2635L, 3099L, 3667L, 4244L, 4537L,
4570L, 4599L, 4631L, 4640L, 4819L, 5394L, 5970L, 6529L, 6793L,
7066L, 7598L, 8113L, 8607L, 10277L, 10610L, 10903L, 11162L, 1460L,
1704L, 1727L, 1728L, 1731L, 1767L, 2530L, 2531L, 2558L, 2591L,
2599L, 2600L, 2601L, 2614L, 4213L, 4504L, 4537L, 4573L, 4598L,
4607L, 4784L, 5355L, 5927L, 6482L, 6745L, 7016L, 7545L, 8056L,
10817L), rfu = c(20855L, 32767L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 14535L, 10201L, 11175L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 32767L, 32767L, 10556L, 10828L,
12400L, 14680L, 17187L, 32767L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 15759L, 15860L, 15713L, 15634L, 14951L,
14881L, 13628L, 13086L, 11895L, 10124L, 10167L, 10645L, 10960L,
12664L, 13541L, 12867L, 31668L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 19641L, 10447L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 32767L, 32767L, 32767L, 32767L,
13635L, 10272L, 11793L, 13124L, 11412L, 14429L, 13771L, 32095L,
29669L, 14818L, 28252L, 32767L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 32767L, 12187L, 15213L, 15434L,
15107L, 15317L, 14560L, 14562L, 13488L, 12820L, 11533L, 10083L,
10523L, 12379L, 13242L, 12629L, 23923L, 32767L, 32767L, 32767L,
32767L, 21650L, 10118L, 32767L, 32767L, 32767L, 32767L, 32767L,
32767L, 10049L, 11715L, 13765L, 11153L, 29499L, 32767L, 32767L,
32767L, 14637L, 14818L, 14733L, 14589L, 13889L, 13749L, 12802L,
12511L, 11508L, 10137L, 11727L, 12512L, 11989L, 21089L, 31649L,
32767L, 32767L, 32767L, 22163L, 32767L, 32767L, 32767L, 32767L,
32767L, 32767L, 32767L, 32767L, 11180L, 10074L, 26622L, 32739L,
32767L, 32767L, 12035L, 12157L, 12177L, 11955L, 11471L, 11481L,
10626L, 10215L, 10009L)), row.names = c(NA, -169L), class = "data.frame")
# Matrix plot of the first colour (channel_1)
ggplot(subset(data, channel=="channel_1"), aes(x=time, y=well)) +
geom_raster(aes(fill=rfu)) +
scale_fill_gradient(low="black", high="blue") +
dark_theme_bw()
# Matrix plot of the second colour (channel_5)
ggplot(subset(data, channel=="channel_5"), aes(x=time, y=well)) +
geom_raster(aes(fill=rfu)) +
scale_fill_gradient(low="black", high="orange") +
dark_theme_bw()
但是有没有可能把所有的颜色都放在同一个图上呢??
使用 ggnewscale
包,它允许多个比例和图例实现相同的审美,你可以这样做:
library(ggplot2)
library(ggnewscale)
library(ggdark)
data_split <- split(data, data$channel)
ggplot(mapping = aes(x = time, y = well)) +
geom_raster(data = data_split[[1]], aes(fill = rfu)) +
scale_fill_gradient(low = "black", high = "blue") +
new_scale_fill() +
geom_raster(data = data_split[[2]], aes(fill = rfu)) +
scale_fill_gradient(low = "black", high = "orange") +
dark_theme_bw()
或者您可以使用例如mapply
像这样为每个通道添加图层:
cols_high <- c("blue", "orange")
ggplot(mapping = aes(x = time, y = well)) +
mapply(function(df, color) {
list(
geom_raster(data = df, aes(fill = rfu)),
scale_fill_gradient(low = "black", high = color),
new_scale_fill()
)
}, data_split, cols_high) +
dark_theme_bw()