gridextra

Align edges of ggplot choropleth (legend title varies)

时间秒杀一切 提交于 2019-11-27 20:50:58
问题 I am attempting to align the left and right edges of 4 ggplot choropleth maps using this method. I am unable to do this though. Original plot: library(ggplot2); library(gridExtra) crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests) states_map <- map_data("state") plot1 <- ggplot(crimes, aes(map_id = state)) + geom_map(aes(fill = Murder), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat) + scale_fill_gradient(low="white", high="darkgreen", name=

How to use empty space produced by facet_wrap?

若如初见. 提交于 2019-11-27 20:50:19
I have a faceted plot that forms an n x m grid. By design, the last (bottom-right) cell is always empty, so I'd like to utilize the extra space by adding another ggplot object. My current solution relies on low-level viewport approach, which is not very elegant and requires some hard-coding for position and size. Instead, I assume the empty space is reachable in some other fashion, probably with gridExtra ? Here's a minimal example for n=m=2 . Note that the edges are not aligned properly, so some extra work is required to manually adjust viewport's parameters, which is a pain, especially if (n

How do I fit a very wide grid.table or tableGrob to fit on a pdf page?

半城伤御伤魂 提交于 2019-11-27 16:08:42
I have a fairly wide table (4/3 of page width) that I'm trying to print using grid.table or grid.arrange (via tableGrob) into a pdf file. The table goes beyond page boundaries and gets clipped. Is there a way to force grid.table/grid.arrange to scale the table to the print area? There is a way, but it's unclear what should happen when the text is too wide to fit in some cells. One option is to set the widths manually, library(grid) library(gridExtra) g1 <- g2 <- tableGrob(head(iris, 10), rows=NULL) g2$widths <- unit(rep(1/ncol(g2), ncol(g2)), "npc") grid.newpage() gt = arrangeGrob(textGrob(

How to add a border around a chart created via arrange.grid in gridExtra encompassing a set ggplot2 scatter plots

烈酒焚心 提交于 2019-11-27 14:06:59
问题 I'm using the code below: # Libs require(ggplot2); require(gridExtra); require(grid) # Generate separate charts chrts_list_scts <- list() # Data data("mtcars") # A chrts_list_scts$a <- ggplot(mtcars) + geom_point(size = 2, aes(x = mpg, y = disp, colour = as.factor(cyl))) + geom_smooth(aes(x = mpg, y = disp), method = "auto") + xlab("MPG") + ylab("Disp") + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.position = "none") # B chrts_list_scts$b

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()

Specify widths and heights of plots with grid.arrange

拟墨画扇 提交于 2019-11-27 11:27:23
I have three plots and I try to combine them with grid.arrange. The last plot should have a smaller height than the first two plots and all the plots should have the same width. A working example: p1 <- qplot(mpg, wt, data=mtcars) p2 <- p1 p3 <- p1 + theme(axis.text.y=element_blank(), axis.title.y=element_blank()) grid.arrange(arrangeGrob(p1,p2, ncol=1, nrow=2), arrangeGrob(p3, ncol=1, nrow=1), heights=c(4,1)) Here, the last plot has a larger width than the first two. In my real data, even if I keep the text and the title on the y-axis, I still have a different width for the third plot. I

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=

Removing one tableGrob when applied to a box plot with a facet_wrap

a 夏天 提交于 2019-11-27 09:19:29
I'm using the code below to enrich a box plot with a summary table for categorical variable created on the x-axis. # Libs require(ggplot2); require(gridExtra); require(grid); require(ggthemes) # Data data(mtcars) # Function to summarise the data fun_dta_sum <- function(var_sum, group, data) { sum_dta <- data.frame( aggregate(var_sum ~ group, FUN = min, data = data), aggregate(var_sum ~ group, FUN = max, data = data), aggregate(var_sum ~ group, FUN = mean, data = data)) sum_dta <- sum_dta[,c(1,2,4,6)] colnames(sum_dta) <- c("Group (x axis)", "min", "max", "mean") rownames(sum_dta) <- NULL sum

ggplot2: Plots over Multiple pages

前提是你 提交于 2019-11-27 09:04:41
I'm plotting graphs using ggplot2 and I have the facet_wrap function to make multiple graphs (~51) but they all appear on one page. Now after searching, I found out that ggplot2 can't place graphs on multiple pages. Is there a way to do this? I looked at this question ( Multiple graphs over multiple pages using ggplot ) and tried out the code, with little success. Here is my code for my graphs, it produces ~51 graphs on one page, making them very small and hard to see, if I could print this to 1 graph per page in a pdf, that would be great: ggplot(indbill, aes(x = prey, y = weight), tab) +

vertical alignment of gridExtra tableGrob (R grid graphics/grob)

不打扰是莪最后的温柔 提交于 2019-11-27 07:57:11
问题 Having some trouble aligning a grid graphics object -- have read all the docs I can find, including the Murrell book, but no success. I think what I'm trying to do is pretty straightforward, so hopefully I'm missing simple. Here's a reproducible example that will make a PDF of all the air carriers by destination in Hadley's hflights package (mirrors what I am trying to do on a different data set). require(hflights) require(gridExtra) require(Cairo) make_table <- function(df) { p <- tableGrob(