我一直在使用ggplot2來繪製一張圖,它看起來像一個熱圖,最右邊的最上面的行和列分別顯示了它的元素總和。然而,這是一個巨大的數據集,所有的「總和」數字都被壓縮到這個數據集中,沒有明確的數據。我想擴大劇情,使標籤看起來可讀。這是我的代碼到目前爲止。如何使用ggplot2增加整個圖的寬度R
mat <- read.csv("trial.csv",sep = ",", header=T, row.names=1)
vec <- as.vector(as.matrix(mat))
varieties = names(mat)
matVar = matrix(vec, ncol = length(varieties), nrow = length(attr(mat, "row.names")))
library(ggplot2)
require(reshape2)
library(grid)
require(grid)
mat$id <- rownames(mat)
gg <- melt(mat)
#mat$id <- reorder(mat$id)
#gg$id<-factor(gg$id)
gg$id <- as.character(gg$id)
gg$id <- factor(gg$id, levels=rev(unique(gg$id)))
ggplot(gg, aes(y=variable,x=id))+
geom_tile(aes(fill=value))+
scale_fill_gradient(low="red",high="white")+
geom_text(aes(label=value), data=cbind(aggregate(value~variable, gg, sum), id="total")) +
geom_text(aes(label=value),data=cbind(aggregate(value~id, gg, sum), variable="total")) +
theme(axis.text.x = element_blank())+
scale_y_discrete(limits=c(levels(gg$variable), "total"))+
scale_x_discrete(limits=c(levels(gg$id), "total"))+
coord_flip()+labs(y = "Patients", x= "Genes")
這裏是示例數據,但我的實際數據集非常龐大。
h1, h2, h3, h4, h5, h6, h7, h8, h9
a, 1, 1, 1, 0, 1, 1, 0, 1
b, 0, 1, 1, 1, 0, 0, 0, 0
c, 1, 0, 0, 1, 1, 1, 1, 1
d, 1, 0, 1, 0, 0, 0, 1, 0
e, 1, 0, 0, 0, 0, 1, 0, 0
f, 1, 1, 0, 0, 0, 0, 0, 0
g, 0, 0, 0, 0, 0, 0, 0, 0
h, 0, 0, 0, 0, 0, 1, 1, 0
這是輸出
謝謝您的答覆。我的問題並不清楚。對不起。我可以完美地得到右手邊的數字。不過,我有問題的數字較高。這可能是因爲我無法修復的情節中的細線。如果你能幫我做到這一點,我會非常感激。 – abn 2014-10-07 22:31:38