Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting a textplot and qplot in same pdf or png in r

Tags:

r

legend

ggplot2

I have a histogram produced using plot in the ggplot2 package:

enter image description here

and a table produced using textplot from the gplots package

enter image description here

I would like to display them in the same pdf or png if possible.

I have tried grid.arrange without luck. Any other suggestions? As each table is only 5 rows long I was also considering inserting it into the plot as a 'legend' or text-box in the margin of the qplot. Any advice on how to make this look good? Thanks for your ideas.

SOLUTION##

In the end I went for and inset unfortunately my colour scheme looks worse...I have posted this issue as a new question.

enter image description here

like image 953
Elizabeth Avatar asked Jul 22 '12 19:07

Elizabeth


Video Answer


2 Answers

Try this,

library(ggplot2)
library(gridExtra)

g = tableGrob(iris[1:2, 1:2])

p = qplot(1:10, 1:10, geom = "blank") +
     annotation_custom(g) # as inset

grid.arrange(p, g, ncol=1) # stacked
ggsave("plot-table.pdf", arrangeGrob(p, g, ncol=1))
like image 129
baptiste Avatar answered Sep 27 '22 21:09

baptiste


Here is an example with the gridExtra library

my_hist<-ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()  
my_table<- tableGrob(head(diamonds)[,1:3],gpar.coretext = gpar(fontsize=8),gpar.coltext=gpar(fontsize=8), gpar.rowtext=gpar(fontsize=8))  
pdf("myplot.pdf")  
grid.arrange(my_hist,my_table, ncol=2)  
dev.off()

You can change the fontsizes in the table, and the size of the pdf: pdf("myplot.pdf",width=10, height = 6)
If you prefer a different arrangment, you might also need a blank panel:
blankPanel<-grid.rect(gp=gpar(col="white")) enter image description here

like image 22
sztup Avatar answered Sep 27 '22 23:09

sztup