我需要使用 ggplot 繪制這個矩陣
one two two one one two
one two one two two one
two one two two one two
所以一個是紅色方塊,兩個是藍色方塊。本質上是一個帶有紅色和藍色方塊的網格。我怎樣才能在 R 中做到這一點?
uj5u.com熱心網友回復:
鑒于你的矩陣:
mat <- matrix(c("one", "two", "two", "one", "one", "two", "one", "two", "one", "two", "two", "one",
"two", "one", "two", "two", "one", "two"), nrow = 3, byrow = TRUE)
> mat
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "one" "two" "two" "one" "one" "two"
[2,] "one" "two" "one" "two" "two" "one"
[3,] "two" "one" "two" "two" "one" "two"
你可以構建一個資料框
mat_df <- reshape2::melt(mat)
然后使用 ggplot2
ggplot(data = mat_df, aes(x = Var1, y = Var2, fill = value))
geom_tile()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/376604.html
