gtable

How to change one specific facet in ggplot

﹥>﹥吖頭↗ 提交于 2019-12-06 05:12:15
问题 I created pie charts displaying different scores (0 - 100%) in ggplot with the help of facet_grid(). However the last score is a total score combining the other scores and in order to distinguish it better from the other scores I would like to change the parameters for this specific facet. Ideally, I would like to make the facet-label bold and move the facet a bit further away from the other facets, but I have no idea how to change the parameters of only one specific facet. library(ggplot2)

Saving plots using grid_draw method instead of gridExtra

▼魔方 西西 提交于 2019-12-05 20:32:33
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 plots in ggplot It worked beautifully for my graphs but when I save the object <- grid.draw(g) the

Vertically align faceted ggplots of different heights when using coord_equal()

拥有回忆 提交于 2019-12-05 11:41:45
I am trying to combine two FACETED ggplot objects with coord_equal() using cowplot::plot_grid() or egg::ggarrange() and vertically align them. The egg::ggarrange() approach works fine for UNFACETED plots, with the solution posted here . However, the egg::ggarrange() solution breaks down when faceting is included. The plots are correctly aligned, but the units of the y-axes are twice as large as those of the x-axes . Any suggestions for how to generalize this for faceting? dat1 <- data.frame(x = rep(1:10, 2), y = 1:20, z = rep(c("A", "B"), 10)) dat2 <- data.frame(x = 1:10, y = 1:10, z = rep(c(

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

回眸只為那壹抹淺笑 提交于 2019-12-04 20:22:06
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 be incorporated at the lower left of the combined plots g <- gtable::gtable_add_rows(g, unit(2,"mm") +

How to change one specific facet in ggplot

我们两清 提交于 2019-12-04 11:32:44
I created pie charts displaying different scores (0 - 100%) in ggplot with the help of facet_grid(). However the last score is a total score combining the other scores and in order to distinguish it better from the other scores I would like to change the parameters for this specific facet. Ideally, I would like to make the facet-label bold and move the facet a bit further away from the other facets, but I have no idea how to change the parameters of only one specific facet. library(ggplot2) df <- data.frame(label = c("A", "B", "Total"), score = c(60, 70, 65)) ggplot(df, aes(x = "", y = score))

Arrange ggplot plots (grobs with same widths) using gtable to create 2x2 layout

拜拜、爱过 提交于 2019-12-03 07:58:23
问题 I am attempting to use grobs and gtable to arrange 4 (ggplot2) plots into a 2x2 grid. I don't know how to set widths, and also a non- 1xn, or nx1 arrangement. Using this code: data(iris) a <- ggplot(iris, aes(x=Species, y=Petal.Width)) + geom_boxplot(color="black") + ylab(expression(Foo~Bar~(g~cm^{-3}))) b <- ggplot(iris, aes(x=Species, y=Petal.Length*100)) + geom_boxplot(color="black") + ylab("foobar (mm)") c <- ggplot(iris, aes(x=Species, y=Sepal.Width)) + geom_boxplot(color="black") + ylab

Create a ggplot2 survival curve with censored table

我怕爱的太早我们不能终老 提交于 2019-12-02 11:24:41
I am trying to create a Kaplan-Meier plot with 95% confidence bands plus having the censored data in a table beneath it. I can create the plot, but not the table. I get the error message: Error in grid.draw(both) : object 'both' not found. library(survival) library(ggplot2) library(GGally) library(gtable) data(lung) sf.sex <- survfit(Surv(time, status) ~ sex, data = lung) pl.sex <- ggsurv(sf.sex) + geom_ribbon(aes(ymin=low,ymax=up,fill=group),alpha=0.3) + guides(fill=guide_legend("sex")) pl.sex tbl <- ggplot(df_nums, aes(x = Time, y = factor(variable), colour = variable,+ label=value)) + geom

Place Annotation at Center of Plot with ggplot2

十年热恋 提交于 2019-12-01 18:34:46
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 plot.list[[length(plot.list)+1]]<-g pdf("MyReport.pdf" ,width = 14 ,height=8.5 ,paper="a4r") for(i in 1

dismantling a ggplot with grid and gtable

拟墨画扇 提交于 2019-12-01 11:15:41
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 lines from one grob would overwrite the data from the other grob. By removing the data lines and points

gtable_add_grob(): linesGrob() not displayed

泄露秘密 提交于 2019-12-01 10:59:41
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) Two reasons: the coordinates fall outside the viewport the rectGrob is drawn on top and masks it require(gtable) # let's fix this name before it's too late gtable_add_grobs <- gtable_add_grob base <- gtable(widths