gtable

ggplot2: Using gtable to move strip labels to top of panel for facet_grid

限于喜欢 提交于 2019-11-28 09:22:50
I am creating a graphic using facet_grid to facet a categorical variable on the y-axis. I decided not to use facet_wrap because I need space = 'free' and labeller = label_parsed . My labels are long and I have a legend on the right so I would like to move the labels from the right of the panel to the top of the panel. Here is an example to show where I'm getting stuck. library(ggplot2) library(gtable) mt <- ggplot(mpg, aes(x = cty, y = model)) + geom_point() + facet_grid(manufacturer ~ ., scales = 'free', space = 'free') + theme_minimal() + theme(panel.margin = unit(0.5, 'lines'), strip.text.y

Annotating facet title as strip over facet

亡梦爱人 提交于 2019-11-28 04:46:34
I want to add a facet title as strip over a facetted plot in ggplot2 . My MWE throws an error. Any help will be highly appreciated. Thanks library(ggplot2) library(gtable) p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() p <- p + facet_grid(. ~ cyl) # get gtable object Plot1 <- ggplot_gtable(ggplot_build(p)) # add label for top strip Plot1 <- gtable_add_rows(Plot1, Plot1$heights[[3]], 2) Plot1 <- gtable_add_grob(Plot1, list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))), textGrob("Cyl", gp = gpar(col = gray(1)))), 3, 4, 3, 10, name = paste(runif(2))) # add margins Plot1 <- gtable_add_rows

ggplot2 2.1.0 broke my code? Secondary transformed axis now appears incorrectly

我只是一个虾纸丫 提交于 2019-11-28 01:09:25
问题 Some time ago, I inquired about adding a secondary transformed x-axis in ggplot, and Nate Pope provided the excellent solution described at ggplot2: Adding secondary transformed x-axis on top of plot. That solution worked great for me, and I returned to it hoping it would work for a new project. Unfortunately, the solution doesn't work correctly in the most recent version of ggplot2 . Now, running the exact same code leads to a "clipping" of the axis title , as well as overlap of the tick

ggplot2 overlay of barplot and line plot

夙愿已清 提交于 2019-11-28 00:31:18
I copied line for line a code from someone on this site on how to overlay two plots with two y axes. However, the example uses two line plots, but I have one line plot and one barplot that I want to overlay. I can't seem to obtain an overlay at all and it just plots the line plot. Please help. Thanks. library(ggplot2) library(gtable) library(grid) require(ggplot2) df1 <- data.frame(frax=c(0,30,60,114),solvb=c(0,0,100,100)) df2 <-data.frame( type = factor(c("mascot","mstat"), levels=c("mascot","mstat")), frax = c(30,35,40,45,50,55), phos=c(542,413,233,500,600,650)) p1<-ggplot(df2,aes(x=frax, y

The perils of aligning plots in ggplot

╄→гoц情女王★ 提交于 2019-11-27 13:16:10
QUESTION How do you combine separate plots (ggplot2), with different y-axis and different plot heights, yet retain alignment? DETAIL When combining plots with grid.arrange (method1), with different y-axis units, they do not align. One way around this is to use gtable (method2), but I cannot adjust the relative height of the plots. EXAMPLE require(ggplot2) #Make two plots, with different y axis x = c(1, 5) y= c(.1, .4) data1<-data.frame(x,y) top<- ggplot(data1, aes(x=x, y=y))+ geom_line() x = c(1, 5) y= c(100000, 400000) data2<-data.frame(x,y) bottom<- ggplot(data2, aes(x=x, y=y))+ geom_line()

Force X axis text on for all facets of a facet_grid plot

戏子无情 提交于 2019-11-27 13:14:22
I have the same problem as this user : I'd like to make a facet_grid plot with a discrete x-axis, and I'd like to have the x-axis labels be written under each facet rather than only underneath the bottom row of facets. For instance: # Drop some factor levels to make the plot smaller diamondSub <- subset(diamonds, (cut=="Ideal" | cut=="Premium") & (color=="E" | color=="I")) # Note that scales="free_x" has no practical effect here ggplot(diamondSub, aes(x=clarity, y=price)) + geom_blank()+ geom_boxplot() + facet_grid(cut~color, scales="free_x") However, I'd prefer not to use the solution from

How to manage the t, b, l, r coordinates of gtable() to plot the secondary y-axis's labels and tick marks properly

被刻印的时光 ゝ 提交于 2019-11-27 11:50:02
问题 I am using facet_wrap and was also able to plot the secondary y-axis. However the labels are not getting plotted near the axis, rather they are plotted very far. I realise it all will get resolved if I understand how to manipulate the coordinate system of the gtable (t,b,l,r) of the grobs. Could someone explain how and what they actually depict - t:r = c(4,8,4,4) means what. There are many links for secondary yaxis with ggplot, however when nrow/ncol is more than 1, they fails. So please

Multiple ggplots of different sizes

三世轮回 提交于 2019-11-27 11:23:11
It's relatively simple using grid.arrange in the gridExtra package to arrange multiple plots in a matrix, but how can you arrange plots (the ones I'm working on are from ggplot2 ) when some plots are intended to be larger than others? In base, I can use layout() such as in the example below: nf <- layout(matrix(c(1,1,1,2,3,1,1,1,4,5,6,7,8,9,9), byrow=TRUE, nrow=3)) layout.show(nf) what is the equivalent for ggplot plots? Some plots for inclusion library(ggplot2) p1 <- qplot(x=wt,y=mpg,geom="point",main="Scatterplot of wt vs. mpg", data=mtcars) p2 <- qplot(x=wt,y=disp,geom="point",main=

R: ggplot2 make two geom_tile plots have equal height

两盒软妹~` 提交于 2019-11-27 06:29:25
问题 I have two ggplot geom_tile plots that I put together using grid.arrange() from the gridExtra library. This is the result library(ggplot2) library(plyr) p3 <- ggplot2(...) p4 <- ggplot2(...) grid.arrange(p3, p4, ncol=2) I would like to get it so that the two plots have the same height and are aligned. I have fiddled around a bit with gtable to alter the dimensions directly but this doesn't seem to work out. gA <- ggplot_gtable(ggplot_build(p3)) gB <- ggplot_gtable(ggplot_build(p4)) maxHeight

ggplot2: Using gtable to move strip labels to top of panel for facet_grid

二次信任 提交于 2019-11-27 02:56:56
问题 I am creating a graphic using facet_grid to facet a categorical variable on the y-axis. I decided not to use facet_wrap because I need space = 'free' and labeller = label_parsed . My labels are long and I have a legend on the right so I would like to move the labels from the right of the panel to the top of the panel. Here is an example to show where I'm getting stuck. library(ggplot2) library(gtable) mt <- ggplot(mpg, aes(x = cty, y = model)) + geom_point() + facet_grid(manufacturer ~ .,