使用@ BrodieG的data.long
,該地塊可能會更容易一些解釋。
library(ggplot2)
library(RColorBrewer) # for brewer.pal(...)
ggplot(data.long) +
geom_bar(aes(x=x, y=success, fill=count),colour="grey70",stat="identity")+
scale_fill_gradientn(colours=brewer.pal(9,"RdYlGn")) +
facet_grid(group~.)
注意,實際值可能是不同的,因爲你的樣品中使用隨機數。將來,請考慮使用set.seed(n)
來生成可重現的隨機樣本。
編輯 [迴應OP的評論]
,因爲你開始矩陣,而不是data.frames您獲得x軸和小標籤號碼。因此,將success
和samples
轉換爲data.frames,將列名稱設置爲您的測試名稱,並在「列表因子」前加上group
列。現在轉換爲長格式有點不同,因爲第一列有組名。設置閾值的顏色
library(reshape2)
set.seed(1)
success <- data.frame(matrix(runif(m*n,0,1),m,n))
success <- cbind(group=rep(paste("Factor",1:nrow(success),sep=".")),success)
samples <- data.frame(floor(MAX*matrix(runif(m*n),m))+1)
samples <- cbind(group=success$group,samples)
data.long <- cbind(melt(success,id=1), melt(samples, id=1)[3])
names(data.long) <- c("group", "x", "success", "count")
一種方法是添加一列data.long
和使用,爲fill
:
threshold <- 25
data.long$fill <- with(data.long,ifelse(count>threshold,max(count),count))
全部放在一起:
library(ggplot2)
library(RColorBrewer)
ggplot(data.long) +
geom_bar(aes(x=x, y=success, fill=fill),colour="grey70",stat="identity")+
scale_fill_gradientn(colours=brewer.pal(9,"RdYlGn")) +
facet_grid(group~.)+
theme(axis.text.x=element_text(angle=-90,hjust=0,vjust=0.4))
最後,當你有x的名字軸標籤往往會卡在一起,所以我旋轉了名稱-90 °。
那麼你想要一個堆疊的條形圖,其中堆棧中的每一層對應於矩陣中的一行?你打算如何繪製3D矩陣(行,列,值),基本上是2d格式(x位置,y高度)。 – BrodieG
我希望成排矩陣的行按行分組......所以,條形圖將有40組高度,每組5個小節。我希望每個欄的顏色編碼爲1到MAX(100)之間的值。你是這個意思嗎? – testname123