gtable

Reproduce a 'The Economist' chart with dual axis

老子叫甜甜 提交于 2019-12-18 03:44:34
问题 I was trying to replicate this chart from The Economist (the one on the left). The chart plots the number of billionaires in Russia on the left y-axis and the number of billionaires in rest of the world on the right. Create the chart for Russian billionaires ( p1 ). Create the chart for the others ( p2 ). Combine p1 and p2 into a dual y-axis chart using the code by Kohske. Data: (content of billionaire.csv ) ,Russia,World 1996,0,423 1997,4,220 1998,1,221 1999,0,298 2000,0,322 2001,8,530 2002

Center-align legend title and legend keys in ggplot2 for long legend titles

六眼飞鱼酱① 提交于 2019-12-17 22:52:17
问题 I am having a hard time making the title of a legend center-aligned relative to the legend keys when the legend title is long. There is a question from a year ago that works for short titles, but it doesn't seem to work for long ones. Example, first with a short legend title: library(ggplot2) ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) + scale_color_distiller(palette = "YlGn", type = "seq", direction = -1, name = "A") + theme(legend.title.align =

Annotating facet title as strip over facet

微笑、不失礼 提交于 2019-12-17 17:38:49
问题 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 =

ggplot2 overlay of barplot and line plot

为君一笑 提交于 2019-12-17 16:40:44
问题 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"

The perils of aligning plots in ggplot

喜你入骨 提交于 2019-12-17 10:49:11
问题 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 =

replacing elements of a ggplot inside a gtable: labels and gridlines

柔情痞子 提交于 2019-12-12 09:53:13
问题 I am learning to manipulate ggplot objects with gtable . [Here is a related question I asked: dismantling a ggplot with grid and gtable ] The present issue is: how to take various axis elements from one gtable and place them into another gtable in replacement of existing elements? and in particular: replace the vertical grid lines (and corresponding tickmarks). Below some code and figures. # Data df <- structure(list(Year = c(1950, 2013, 1950, 2013), Country = structure(c(1L, 1L, 2L, 2L),

Draw a line across multiple ggplot figures in a gtable_matrix

偶尔善良 提交于 2019-12-11 07:33:52
问题 I am trying to draw a line across two ggplot histograms in a gtable_matrix, so that the mean of the values in the one histogram is overlaid across both plots. However, I cannot get at the device coordinates of the plotting area. In base graphics, I would use grconvertX(), but where can I find the device coordinates of the plotting area of ggplot so I can convert numbers on my 'user' scale (0-10) to device coordinates? In the example below, I have meticulously found the numbers to plug in to

dual y axis together with facet_wrap

回眸只為那壹抹淺笑 提交于 2019-12-10 15:58:34
问题 At How can I put a transformed scale on the right side of a ggplot2? it was shown how to add two y-axis in the same plot by manipulation and merging ggplot2 objects with gtable. From the example there I managed to extend it to work with facet_wrap. See the example below. There are however three things that are not perfect. The scale is always put on the far right. It would be better if it connected with the plot in the last row It doesn't work if there is y-axis on all plots separately (i.e.

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

老子叫甜甜 提交于 2019-12-07 05:02:26
问题 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

How to set legend height to be the same as the height of the plot area?

强颜欢笑 提交于 2019-12-07 02:09:06
问题 I have arranged two plots: a line chart on top and a heatmap below. I want the heatmap legend to have the same height as the plot area of the heatmap, i.e. the same length as the y-axis. I know that I can change the height and size of the legend using theme(legend.key.height = unit(...)) , but this would take many trial and errors before I find an adequate setting. Is there a way to specify the height of the legend so that it is exactly the same height of the plot area of the heatmap and