gtable

How to use gtable_add_grob() does not 'add'

此生再无相见时 提交于 2019-12-23 18:01:05
问题 Why does the following plot not display the numbers ( g ; specified via textGrob(label=g)) ) in the 6 panels? If I use the text grob only, this also works, but a text grob and a rectangular grob seem to be not so easy. Unfortunately the help page of gtable_add_grob does not give a lot of help... require(gtable) base <- gtable(widths=unit(rep(1, 2), "null"), heights=unit(rep(1, 3), "null")) g <- 1 for(i in 1:3) { for(j in 1:2) { base <- gtable_add_grob(base, list(rectGrob(gp=gpar(fill="

Changing height of strip text background in ggplot2 does not work as expected

回眸只為那壹抹淺笑 提交于 2019-12-23 10:44:46
问题 ###Load libraries library(ggplot2) library(gtable) ###Build plot d <- ggplot(mtcars, aes(x=gear)) + geom_bar(aes(y=gear), stat="identity", position="dodge") + facet_wrap(~cyl) ###Change height of strip text g <- ggplotGrob(d) g$heights[[3]] = unit(2,"in") grid.newpage() grid.draw(g) Obtained result ( ggplot2_2.0.0 ) Expected result ( ggplot2_1.0.1 ) Question What in middle earth is going on here? 回答1: This seems to do the trick g <- ggplotGrob(d) g$heights[[3]] = unit(2,"in") g$grobs[[5]]

How to align two plots with ggplot?

ⅰ亾dé卋堺 提交于 2019-12-22 12:53:55
问题 I a trying to align two plots using grid but with no success. I have tried tweaking the themes so the plot border/size is the same but the plots do not align despite using the same y-coordinates. For the example below i could use annotation_custom (a few examples on site) but this limits the quantity of text I can add. Any suggestions / amendments / alternatives are appreciated. My ugly code library(gridExtra) library(ggplot2) # Data my.df<-data.frame(vars=paste("variable",letters[1:8],sep="-

Saving plots using grid_draw method instead of gridExtra

本小妞迷上赌 提交于 2019-12-22 09:46:24
问题 I have used gridExtra to create 2 plots next to each other and I can save the object using ggsave However, the plots are misaligned in gridExtra so I used this method #Method 2 - gtable require(gtable) #Extract Grobs g1<-ggplotGrob(left) g2<-ggplotGrob(right) #Bind the tables g<-gtable:::cbind_gtable(g1, g2, "first") #Remove a row between the plots g <- gtable_add_cols(g, unit(-1,"cm"), pos=ncol(g1)) #draw grid.newpage() grid.draw(g) this method is covered in this link The perils of aligning

“\n” command not working to make 2 or 3-line long figure caption using textGrob

我的未来我决定 提交于 2019-12-22 00:04:25
问题 I have been trying to make a 2 to 3-line figure caption below the combined plots using the following script: library(grid) library(gridExtra) library(ggplot2) g1 <- ggplotGrob(pl) #pl is my plot 1 g2 <- ggplotGrob(pl1) #pl1 is my plot 2 g <- rbind(g1, g2) #this combines my two plots caption <- textGrob(expression(paste(bold("Figure 1"), ". This is the first line with a", italic( " scientific name."),"\nThis should be the second line.","\nThis is the third line.")), hjust=0, x=0) #caption to

Place Annotation at Center of Plot with ggplot2

烈酒焚心 提交于 2019-12-20 01:37:21
问题 I'd like to place an annotation at the center of a several ggplot objects. I've researched and found a few similar questions such as here: Relative positioning of geom_text in ggplot2? So far, the only answer I've found is to manipulate the absolute range (e.g. ",y = ymax/2"). I'd like to add the annotation layer in a loop, prior to printing to .pdf. I can place the annotation in the corners by using +/- Inf, as follows: plot.list<-list() g<- qplot(1,1) plot.list[[length(plot.list)+1]]<-g

Force x-axis labels on facet_grid ggplot: x-axis labels differ per row

五迷三道 提交于 2019-12-19 09:03:18
问题 I was so happy to find the greater part of a solution to my question in the post, "Force X axis text on for all facets of a facet_grid plot" . I'd like to create a graph to look somewhat like the OP Drew Steen's, except I have more than two rows of facets, and I'd like to make the x-axes labels different for each row. I made a super-hacky solution out of @baptiste's awesome answer (mostly because I am unfamiliar with the gtable package), and I'd like to know: If there is a more elegant

aligning distinct non-facet plots in ggplot2 using Rpy2 in Python

人走茶凉 提交于 2019-12-18 15:00:36
问题 I am combining two distinct plots into a grid layout with grid as suggested by @lgautier in rpy2 using python. The top plot is a density and and the bottom a bar graph: iris = r('iris') import pandas # define layout lt = grid.layout(2, 1) vp = grid.viewport(layout = lt) vp.push() # first plot vp_p = grid.viewport(**{'layout.pos.row': 1, 'layout.pos.col':1}) p1 = ggplot2.ggplot(iris) + \ ggplot2.geom_density(aes_string(x="Sepal.Width", colour="Species")) + \ ggplot2.facet_wrap(Formula("~

make panels with same margins when combining ggplot and base graphics

允我心安 提交于 2019-12-18 12:33:31
问题 I have generated a figure that combines ggplot and base graphics: t <- c(1:(24*14)) P <- 24 A <- 10 y <- A*sin(2*pi*t/P)+20 #***************************************************************************** par(mfrow = c(2,1)) plot(y,type = "l",xlab = "Time (hours)",ylab = "Amplitude") aa <- par("mai") plot.new() require(gridBase) vps <- baseViewports() pushViewport(vps$figure) pushViewport(plotViewport(margins = aa)) ## I use 'aa' to set the margins #*********************************************

ggplot grobs align with tableGrob

孤者浪人 提交于 2019-12-18 06:58:27
问题 I'm having difficulty to find solution for aligning ggplot grob and table grob. I tried to follow the instruction here but still didn't give the results I wanted. library(grid) library(gridExtra) library(ggplot2) library(tibble) library(gtable) dat <- tibble::rownames_to_column(mtcars, "car") #convert rownames to first col plot1 <- ggplot(dat, aes(car, mpg)) + geom_bar(stat = "identity") + coord_flip() g1 <- ggplotGrob(plot1) tb1 <- tableGrob(dat$cyl) g1 <- gtable_add_cols(g1, unit(0.2, "npc"