I have a histogram produced using plot in the ggplot2 package:
and a table produced using textplot from the gplots package
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.
In the end I went for and inset unfortunately my colour scheme looks worse...I have posted this issue as a new question.
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))
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With