How to increase the width of an entire plot using ggplot2 R
I am using ggplot2 to plot a graph that looks like a heatmap with the largest and top number of rows and columns respectively displaying the sum of its elements. However, this is a huge dataset, and all "totals" numbers are compressed into it without clarity. I would like to expand on the plot to make the labels look readable. Here is my code so far.
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")
Here's the sample data, but my actual dataset is really huge.
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
This is the result
source to share
Try to specify the dimensions of your graphics device, for example.
pdf(file="plot.pdf",width=11,height=8)
PDF uses dimensions in inches and PNG uses dimensions in pixels, so you might need to specify a resolution as well, eg.
png(file="animals45.png",width=400,height=350,res=45)
for more information see:
source to share
Here's one way, although it's a bit of a hack. Since you are using factors and you have disabled the horizontal axis, you can add additional labels to scale_y_discrete(...)
.
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=123456789*value),data=cbind(aggregate(value~id, gg, sum), variable="total"),hjust=0) +
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")
source to share