gridextra

How can I add a title to a tableGrob plot?

强颜欢笑 提交于 2019-11-30 04:25:34
问题 I have a table, and I want to print a title above it: t1 <- tableGrob(top_10_events_by_casualties, cols=c("EVTYPE", "casualties"), rows=seq(1,10)) grid.draw(t1) A similar question was asked here: Adding text to a grid.table plot I've tried something similar and it doesn't work: > title <- textGrob("Title",gp=gpar(fontsize=50)) > table <- gtable_add_rows(t1, + heights = grobHeight(title) + padding, + pos = 0) Error: is.gtable(x) is not TRUE 回答1: Not sure what the problem was, but here is a

ggplot2: Define plot layout with grid.arrange() as argument of do.call()

痴心易碎 提交于 2019-11-30 03:40:44
I want to obtained an unbalanced grid of plots such as require(ggplot2) require(gridExtra) df <- data.frame(value1 = rnorm(200), value2 = rnorm(200), value3 = rnorm(200), value4 = rnorm(200)) p1 <- ggplot(df) + geom_density(aes(x=value1)) p2 <- ggplot(df) + geom_density(aes(x=value2)) p3 <- ggplot(df) + geom_density(aes(x=value3)) p4 <- ggplot(df) + geom_density(aes(x=value4)) grid.arrange(p1, arrangeGrob(p2,p3,p4, ncol=3), heights=c(2.5/4, 1.5/4), ncol=1) but using a function myplot <- function(i){ p <- ggplot(df) + geom_density(aes_string(x=i)) return(p) } and an lapply call p <- lapply(c(

ggplot2: multiple plots in a single row with a single legend

佐手、 提交于 2019-11-30 03:13:45
问题 I want a combined plot of two plots + their legend like this: library(ggplot2) library(grid) library(gridExtra) dsamp <- diamonds[sample(nrow(diamonds), 1000), ] p1 <- qplot(price, carat, data=dsamp, colour=clarity) p2 <- qplot(price, depth, data=dsamp, colour=clarity) g <- ggplotGrob(p1 + theme(legend.position="bottom"))$grobs legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]] grid.arrange(arrangeGrob(p1+theme(legend.position="right"),p2+theme(legend.position="none"),legend

Merging Table Header Cells Using tableGrob

时光总嘲笑我的痴心妄想 提交于 2019-11-30 03:11:13
问题 I am currently creating a table image using: SummaryTable <- data.frame( index, xa, xb, ya, yb, za, zb ) names(SummaryTable) <- c("Index", "Main X Sub A", "Main X Sub B", "Main Y Sub A", "Main Y Sub B", "Main Z Sub A", "Main Z Sub B") tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE))) tbl <- tableGrob(SummaryTable, rows=NULL, theme=tt) grid.arrange(tbl,as.table=TRUE) With output: Using dput( SummaryTable ): structure(list(Index = 0:8, `Main X Sub A` = c(1, 0.69, 0.61, 0.56, 0.5,

R tableGrob change format of row

£可爱£侵袭症+ 提交于 2019-11-29 23:21:53
问题 I have some relatively simple code to create a table for printing to a PDF: library(gridExtra) df <- head(iris) tableGrob(df, gp = gpar(fontsize = 8), rows = NULL) I'd like to make the last row the same format as the header row (bold, and darker gray background). I understand I can use gpar to control format of the entire table, but not sure how to just affect the last row. Thanks! 回答1: One option is to create a new table, and merge the two together, g1 <- tableGrob(iris[1:4, 1:3], rows=NULL)

Arrange common plot width with facetted ggplot 2.0.0 & gridExtra

核能气质少年 提交于 2019-11-29 20:24:45
问题 Since I have updated to ggplot2 2.0.0, I cannot arrange charts propperly using gridExtra. The issue is that the faceted charts will get compressed while other will expand. The widths are basically messed up. I want to arrange them similar to the way these single facet plots are: left align two graph edges (ggplot) I put a reproducible code library(grid) # for unit.pmax() library(gridExtra) plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + facet_grid(. ~ Species) +

Arrange ggplots together in custom ratios and spacing

余生颓废 提交于 2019-11-29 08:59:07
问题 I am trying to combine n number of barplots with one common label plot at the bottom. My problem is that grid.arrange combines the two plots in 50%-50%. I am looking for something like the layout matrix where you can specify 4 slots and first 3 to be taken up by first plot and last slot by second plot. And customise similarly depending on number of plots. I have a sample code here from what I am trying: #load libraries require(ggplot2) require(reshape) require(grid) require(gridExtra) #data

R adding a datatable to a ggplot graph using viewPorts : Scaling the Grob

不羁岁月 提交于 2019-11-29 08:44:33
问题 I'm trying to add a data-table to a graph made in ggplot (similar to the excel functionality but with the flexibility to change the axis its on) I've had a few goes at it and keep hitting a problem with scaling so attempt 1) was library(grid) library(gridExtra) library(ggplot2) xta=data.frame(f=rnorm(37,mean=400,sd=50)) xta$n=0 for(i in 1:37){xta$n[i]<-paste(sample(letters,4),collapse='')} xta$c=0 for(i in 1:37){xta$c[i]<-sample((1:6),1)} rect=data.frame(xmi=seq(0.5,36.5,1),xma=seq(1.5,37.5,1

Global legend using grid.arrange (gridExtra) and lattice based plots

喜欢而已 提交于 2019-11-29 08:25:21
I am producing four plots using xyplot (lattice) and further combine them with grid.arrange (gridExtra). I would like to obtain a graph with a common global legend. The closest that I have reached is the following. They have to be in a matrix layout, otherwise an option would be to put them in a column and include only a legend for the top or bottom one. # Load packages require(lattice) require(gridExtra) # Generate some values x1<-rnorm(100,10,4) x2<-rnorm(100,10,4) x3<-rnorm(100,10,4) x4<-rnorm(100,10,4) y<-rnorm(100,10,1) cond<-rbinom(100,1,0.5) groups<-sample(c(0:10),100,replace=TRUE)

R: remove repeating row entries in gridExtra table

霸气de小男生 提交于 2019-11-29 07:20:59
Problem: I create a table using the gridExtra package: require("gridExtra") # Prepare data frame col1 = c(rep("A", 3), rep("B", 2), rep("C", 5)) col2 = c(rep("1", 4), rep("2", 3), rep("3", 3)) col3 = c(1:10) df = data.frame(col1, col2, col3) # Create table grid.arrange(tableGrob(df, show.rownames=F)) The output: Question: I would like to get rid of the repeating row entries and achieve spanning entries which look like this (this image is a mockup made with Photoshop): Any ideas how to achieve this programmatically in R? I would use gtable, and take advantage of its more flexible framework,