I'm making a kind of heatmap using geom_raster
, and want to set the intensity using fill
, and the colour using scale_fill_gradient
. My data is grouped and I'd like to use facet_wrap
to split up the plots. I would like the intensity scale (fill
) to be independent in each facet.
I can make the plots, but am having trouble mapping the colour with aes
, and with making the fill
independent in each facet.
Here's a small demo dataset.
data <- structure(list(name = 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", "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"), scan = c(957,
958, 959, 960, 1126, 1127, 1128, 1129, 1130, 1131, 1132, 1133,
1134, 1134, 1135, 1135, 1136, 1136, 1137, 1137, 1138, 1138, 1139,
1139, 1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 2727, 2728,
2729, 2730, 2731, 2732, 2733, 953, 954, 955, 956, 957, 1122,
1123, 1124, 1125, 1126, 1127, 1127, 1128, 1128, 1129, 1129, 1130,
1130, 1131, 1131, 1132, 1132, 1133, 1133, 1134, 1135, 1136, 1137,
1138, 1139, 1156, 1157, 2556, 2557, 2558, 2559, 2560), channel = c("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_5",
"channel_1", "channel_5", "channel_1", "channel_5", "channel_1",
"channel_5", "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_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_1",
"channel_1", "channel_1", "channel_1", "channel_1", "channel_5",
"channel_1", "channel_5", "channel_1", "channel_5", "channel_1",
"channel_5", "channel_1", "channel_5", "channel_1", "channel_5",
"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"
), rfu = c(10330L, 11210L, 11395L, 11034L, 10417L, 11494L, 13529L,
18957L, 31897L, 32767L, 32767L, 32767L, 32767L, 18670L, 32767L,
26552L, 32767L, 27678L, 32767L, 27786L, 32767L, 26860L, 32767L,
21572L, 32767L, 32767L, 32767L, 32767L, 19650L, 13961L, 11675L,
10285L, 11510L, 17900L, 23551L, 26566L, 24682L, 18946L, 11961L,
10449L, 11304L, 11491L, 11180L, 10171L, 14065L, 25044L, 32767L,
32767L, 32767L, 32767L, 15007L, 32767L, 25165L, 32767L, 27175L,
32767L, 27895L, 32767L, 27604L, 32767L, 26180L, 32767L, 19362L,
32767L, 32767L, 32767L, 27058L, 15568L, 10670L, 10094L, 10053L,
13930L, 17501L, 18395L, 15883L, 11368L), plotcolour = c(channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_5 = "orange", channel_1 = "blue", channel_5 = "orange",
channel_1 = "blue", channel_5 = "orange", channel_1 = "blue",
channel_5 = "orange", channel_1 = "blue", channel_5 = "orange",
channel_1 = "blue", channel_5 = "orange", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_5 = "orange", channel_1 = "blue",
channel_5 = "orange", channel_1 = "blue", channel_5 = "orange",
channel_1 = "blue", channel_5 = "orange", channel_1 = "blue",
channel_5 = "orange", channel_1 = "blue", channel_5 = "orange",
channel_1 = "blue", channel_5 = "orange", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue",
channel_1 = "blue", channel_1 = "blue", channel_1 = "blue", channel_1 = "blue"
)), row.names = c(NA, -76L), class = "data.frame")
library(ggplot2)
ggplot(data, aes(x=scan, y=name, fill=rfu)) +
geom_raster() +
scale_fill_gradient(aes(low="black", high=plotcolour)) +
facet_wrap(~channel)
I think you'd be better off with a list of plots rather than a facet:
library(patchwork)
library(ggplot2)
ggList <- lapply(split(data, data$channel), function(i) {
ggplot(i, aes(x = scan, y = name, fill = rfu, group = channel)) +
geom_raster() +
scale_fill_gradient(low = "black", high = unique(i["plotcolour"]))
})
ggList[[1]] + ggList[[2]]