我正在嘗試創建以下情節的熱圖。每當我使用熱圖函式而不是 geom_point 時,我都會收到此錯誤:
"Error in heatmap(Wizard_heatmap, aes(x = Gate)) : 'x' must be a numeric matrix"
資料框
Wizard_heatmap <- structure(list(Response = c("LIZARD", "LIZARD", "LIZARD", "NR",
"NR", "WAITER", "WEEKEND", "WHALE", "WHEELCHAIR", "WHIP", "WHISKEY",
"WHISTLE", "WHISTLE", "WHISTLE", "WIND", "WINDMILL", "WINDOW",
"WINDOW", "WINTER", "WISDOM", "WISDOM", "WIZARD", "WIZARD", "WIZARD",
"WIZARD", "WOMEN", "WOOD", "WOODCHIP"),
Target = c("WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD",
"WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD",
"WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD",
"WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD"),
Gate = c("Full", "Gate 2", "Gate 3", "Gate 0", "Gate 1",
"Gate 0", "Gate 1", "Gate 0", "Gate 0", "Gate 0", "Gate 1",
"Gate 0", "Gate 1", "Gate 2", "Gate 0", "Gate 0", "Gate 0",
"Gate 1", "Gate 0", "Gate 1", "Gate 2", "Full", "Gate 1",
"Gate 2", "Gate 3", "Gate 0", "Gate 0", "Gate 0"),
n = c(1, 2, 2, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 7,
1, 15, 1, 12, 14, 1, 1, 1)), row.names = c(NA, -28L), class = c("tbl_df", "tbl", "data.frame"))
簡單的情節
ggplot(Wizard_heatmap, aes(x = Gate))
geom_point(aes(y = Response), size = 4, color = "red")
xlab("Gate")
這是為了進行錯誤分析,我們試圖查看錯誤集中在哪里。我們還想了解錯誤發生在哪里,這就是 x 軸按門排序的原因。最后,在 df 中,您將看到其中一列標記為“n”。較大的“n”在熱圖上應該更暗。提前致謝!
uj5u.com熱心網友回復:
這是另一個使用tidyverse包的解決方案,需要一些預處理來獲取回應和門的所有組合,然后制作熱圖。
library(tidyverse)
Wizard_heatmap |>
# Get all combinations of Response and Gate
expand(Response, Gate) |>
# Left join with original data frame; new entries will get NA's
left_join(Wizard_heatmap,
by = c("Response", "Gate")) |>
# Ggplot and set the order of x and y factors
ggplot(aes(x = fct_inorder(Gate),
y = Response,
fill = n))
# Make tile plot
geom_tile()
# Change labs
labs( x = "Gate",
y = "Response")
# Override default fill
scale_fill_distiller(type = "seq",
palette = "Reds",
direction = 1,
# Set na values color in hexadecimal code
na.value = "#FBF2F0")

uj5u.com熱心網友回復:
如果你想要這個資料集的熱圖,你可以這樣做:
heatmap(with(Wizard_heatmap, as.matrix(table(factor(Response), factor(Gate)))))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450627.html
下一篇:在Rggplot中用小數繪制年份
