แผนที่ความร้อน R ggplot ที่มีหลายแถวที่มีตำนานแยกกันบนกราฟเดียวกัน
ฉันกำลังพยายามสร้างแผนที่ความร้อนโดยใช้ ggplot2 ที่มีตัวแปร 3 ประเภทซึ่งแต่ละประเภทต้องการตำนาน / มาตราส่วนอิสระของตนเอง
ฉันสามารถพล็อตทั้งหมดในแผนที่ความร้อนเดียว (ภาพด้านล่าง) แต่ฉันมีปัญหาในการแยกพวกเขาเพื่อให้มีตำนานของตัวเอง สามหมวดหมู่ของฉันคือแถว "Score", "samp1" และข้อมูลที่เหลือ ฉันอยากให้แต่ละคนมีตำนานอิสระของตัวเองตามช่วงต่างๆ
การเพิ่มเพียงอย่างเดียวของฉันคือการให้คะแนนแถวมีโทนสีเขียวเหลืองแดง (ต่ำกลางสูง) หากเป็นไปได้ที่จะรวมไว้ในคำถามนี้

นี่คือรหัสที่ฉันใช้สร้างกราฟนั้น
library(ggplot2)
test_data <- read.csv("test_table.csv", row.names = 1)
ggplot(test_data, aes(x=sample, y=id, fill = value)) +
geom_raster() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
strip.text.y = element_blank()) + #remove facet bar on y
scale_fill_gradient(low = "darkblue", high = "lightblue") +
ggtitle("test table") +
facet_grid(rows = vars(test_data$category), cols = vars(test_data$group), scales = "free", space="free_y") #facets to add gaps
ฉันได้ใช้แง่มุมเพื่อแยกข้อมูลตามกลุ่มตัวอย่างและตาม 3 หมวดหมู่ที่ฉันอธิบายไว้ข้างต้น ฉันหวังว่าจะใช้การจัดกลุ่มนี้เพื่อสร้างตำนานของตัวเองเช่นกัน แต่ฉันไม่แน่ใจว่าเป็นไปได้หรือไม่
คลิกที่นี่เพื่อดาวน์โหลดข้อมูล (ก่อนละลาย)
ขอบคุณล่วงหน้า.
คำตอบ
สิ่งนี้สามารถทำได้ผ่านggnewscale
แพ็คเกจดังนี้:
library(ggplot2)
library(dplyr)
library(ggnewscale)
ggplot() +
geom_raster(data = filter(test_data, category == "1 score"), aes(x = sample, y = id, fill = value)) +
scale_fill_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 4, name = "Score") +
new_scale_fill() +
geom_raster(data = filter(test_data, category == "2 samp1"), aes(x = sample, y = id, fill = value)) +
scale_fill_gradient(low = "darkblue", high = "lightblue", name = "Sample1") +
new_scale_fill() +
geom_raster(data = filter(test_data, category == "3 samp2"), aes(x = sample, y = id, fill = value)) +
scale_fill_gradient(low = "darkblue", high = "lightblue", name = "Sample2") +
ggtitle("test table") +
facet_grid(
rows = vars(category),
cols = vars(group), scales = "free", space = "free_y"
) +
theme(
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.text.y = element_blank()
)
ฉันขอแนะนำแนวทางต่อไป แยกข้อมูลตามกลุ่มแล้วสร้างพล็อตแยกสำหรับแต่ละกลุ่มด้วยฟังก์ชัน สุดท้ายใช้purrr
และpatchwork
เข้าร่วมแผนการทั้งหมดกับตำนานที่แตกต่างกัน นี่คือรหัส:
library(purrr)
library(ggplot2)
library(patchwork)
#Load data
test_data <- read.csv("test_table.csv", row.names = 1)
#Split into list
List <- split(test_data,test_data$group) #Function for plots myfun <- function(x) { G <- ggplot(x, aes(x=sample, y=id, fill = value)) + geom_raster() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical strip.text.y = element_blank()) + #remove facet bar on y scale_fill_gradient(low = "darkblue", high = "lightblue") + facet_grid(rows = vars(x$category),
cols = vars(x$group), scales = "free", space="free_y")
return(G)
}
#Apply
List2 <- lapply(List,myfun)
#Plot
reduce(List2, `+`)+plot_annotation(title = 'My plot')
ผลลัพธ์:

คุณสามารถสำรวจเพิ่มเติมเกี่ยวกับpatchwork
และวิธีการเข้าร่วมหลายแปลง
เป็นไปได้อย่างแน่นอนที่จะสร้าง 3 พล็อตและรวมเข้าด้วยกัน แต่เนื่องจากแง่มุมเป็นรูปทรงที่แตกต่างกันจึงไม่เหมาะอย่างยิ่ง คุณสามารถใช้ggnewscale
แพ็คเกจตาม Stefan ซึ่งง่ายกว่าตอนนี้อยู่ใน CRAN แต่ถ้าคุณต้องการทำใน ggplot เดียวโดยไม่มีส่วนเสริมก็เป็นไปได้ คุณแค่ต้องพล็อตgeom_point
ที่ทำจากสี่เหลี่ยมทึบขนาดใหญ่ ซึ่งจะช่วยให้คุณใช้สเกลสีและสเกลการเติม
ggplot(test_data, aes(x=sample, y=id, fill = value)) +
geom_raster() +
geom_point(aes(alpha = id, colour = value), size = 12, shape = 15) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
strip.text.y = element_blank()) +
scale_fill_gradient(low = "darkblue", high = "lightblue") +
ggtitle("test table") +
facet_grid(rows = vars(test_data$category), cols = vars(test_data$group), scales = "free", space = "free_y") +
scale_alpha_manual(values = c(rep(0, 19), 1, 0, 0), guide = guide_none()) +
scale_color_gradientn(colours = c("red", "orange", "gold", "yellow"))
