r-grid

R - adding page numbers to PDF

隐身守侯 提交于 2019-12-01 09:27:35
I'm having trouble adding page numbers to PDFs. Here's how I'm inserting pages / plots: pdf( file = pdfFilePath , width = 11 , height = 8.5 ) for ( ... ) { grid.newpage() pushViewport( viewport( layout = grid.layout( 2 , 2 ) ) ) ... print 4 plots .... } onefile seems to name a file by the page number, but I want the page numbers to appear in the same file. Edit I've modified @Gavin's code sample to produce a working version of mixing graphic types to get page numbers: require(ggplot2) pdf( file = "FILE_PATH_TO_SAVE_PDF_HERE" , width = 11 , height = 8.5 ) par( oma = c ( 4 , 4 , 4 , 4 ) , mar=c(

dismantling a ggplot with grid and gtable

徘徊边缘 提交于 2019-12-01 07:42:39
问题 I'm struggling to build a dual-axis plot based on ggplot objects. At baptiste's suggestion, I have broken down the problem into smaller parts. The present issue is: how to remove all of the data from the grobs , while keeping the axis, the axis labels, the axis tickmarks, and the grid lines? By 'data' I mean the data associated with geom_line() and geom_points() . The reason for wanting to do this is that in the process of building a dual-axis plot, I ran into the following problem: the grid

gtable_add_grob(): linesGrob() not displayed

狂风中的少年 提交于 2019-12-01 07:40:49
问题 Why are the lines (via linesGrob ) not drawn in the following plot? require(gtable) base <- gtable(widths=unit(rep(1, 2), "null"), heights=unit(rep(1, 3), "null")) grid.newpage() g <- 1 for(i in 1:3) { for(j in 1:2) { base <- gtable_add_grob(base, grobs=list(linesGrob(x=1:4, y=4:1), rectGrob(gp=gpar(fill="#FF0000")), textGrob(label=g)), i, j, name=1:3) g <- g+1 } } grid.draw(base) 回答1: Two reasons: the coordinates fall outside the viewport the rectGrob is drawn on top and masks it require

Removing right border from ggplot2 graph

好久不见. 提交于 2019-12-01 07:39:52
问题 With the following code I can remove top and right borders along with other things. I wonder how to remove the right border of the ggplot2 graph only. p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() p + theme_classic() 回答1: the theme system gets in the way, but with a little twist you can hack the theme elements, library(ggplot2) library(grid) element_grob.element_custom <- function(element, ...) { segmentsGrob(c(1,0,0), c(0,0,1), c(0,0,1), c(0,1,1), gp=gpar(lwd=2)) } ## silly

R - adding page numbers to PDF

孤街浪徒 提交于 2019-12-01 05:46:37
问题 I'm having trouble adding page numbers to PDFs. Here's how I'm inserting pages / plots: pdf( file = pdfFilePath , width = 11 , height = 8.5 ) for ( ... ) { grid.newpage() pushViewport( viewport( layout = grid.layout( 2 , 2 ) ) ) ... print 4 plots .... } onefile seems to name a file by the page number, but I want the page numbers to appear in the same file. Edit I've modified @Gavin's code sample to produce a working version of mixing graphic types to get page numbers: require(ggplot2) pdf(

Side-by-side Venn diagram using Vennerable

折月煮酒 提交于 2019-11-30 21:53:09
I am trying to put two Venn diagrams in one single graph, i.e. I am using par(mfrow=c(1,2)) at the very beginning. However, when I use the Venn() function in the Vennerable package: VennCompare = Venn(SetNames = c("A", "B", "C"), Weight = c(0, 38, 1, 0, 1, 80, 0, 14)) pdf(file="Venn.pdf", width=12, height=6) par(mfrow=c(1,2)) plot(VennCompare, doWeights=FALSE) plot(VennCompare, doWeights=TRUE, show = list(SetLabels = TRUE, Faces = FALSE)) dev.off() The resultant pdf file contains 2 pages, and each page has a single Venn diagram. How can I put the two diagrams into a single page (i.e. side-by

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

本小妞迷上赌 提交于 2019-11-30 06:52:01
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),ymi=0,yma=10) xta=cbind(xta,rect) a = ggplot(data=xta,aes(x=n,y=f,fill=c)) + geom_bar(stat='identity'

grid.arrange ggplot2 plots by columns instead of by row using lists

≯℡__Kan透↙ 提交于 2019-11-30 05:40:37
问题 I want create a multiplot of ggplot2 plots from a list using grid.arrange but arrange them by columns before doing it by rows. gg_list1 <- list(qplot(mpg, disp, data = mtcars), qplot(hp, wt, data = mtcars), qplot(qsec, wt, data = mtcars)) gg_list2 <- list(qplot(mpg, disp, data = mtcars), qplot(hp, wt, data = mtcars), qplot(qsec, wt, data = mtcars)) I know I can do this: do.call(grid.arrange,c(gg_list1,gg_list2 , ncol = 2, nrow = 3)) but it fills from left to right before top to bottom. I've

How to use R base plots in grid.newpage?

ⅰ亾dé卋堺 提交于 2019-11-30 05:09:10
问题 Is it possible to "force" R base plots in grid package's grid.newpage ? For example, this works fine: library(grid) grid.newpage() vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom")) vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom")) pushViewport(vp1) grid.rect() grid.text("vp1", 0.5, 0.5) upViewport() pushViewport(vp2) grid.rect() grid.text("vp2", 0.5, 0.5) . But if I try something like this: grid.newpage() vp1 <- viewport(x=0,y=0.5,width=0

R YaleToolkit: How to change the font size of tick labels on the sparklines?

筅森魡賤 提交于 2019-11-30 00:23:49
I'm using this function for some quick and easy sparklines with R but I can't seem to work out how to change the font size to avoid ugly overlaps of the y-axis tick labels. Here's my code (see below for a reproducible example): sparklines(gamma.df, sub=c(1:23),outer.margin = unit(c(2, 2, 2, 2), "cm")) and the resulting plot: I seem to be able to completely suppress the y-axis with sparklines(gamma.df, sub=c(1:23),yaxis=FALSE,outer.margin = unit(c(2, 2, 2, 2), "cm")) But what I really want is just shrink the numbers at the tick marks (and add grey fill under the line, but it looks like I'd have