Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set ggplot plots to have same x-axis width and same space between dot plot rows

Updated question to incorporate a partial solution already answered on SO

I am using ggplot2 to create several plots and gridExtra to combine the plots into one figure with several panels, all in one column. My problem is that I can't get the space between the dot plot rows to be consistent in both plots.

enter image description here

library(ggplot2)
# data
  dat1 <- data.frame(VARIABLES=c("Item 1", "Item 2 is a little longer"),
                     est=c(.3, .5),
                     min=c(.2, .4),
                     max=c(.4, .7))
  dat2 <- data.frame(VARIABLES=c("Item 3", 
                                 "Item 4 is even longer if you can believe it",
                                 "And there is a third item",
                                 "And a fourth item"),
                     est=c(.3, .5, .3, .5),
                     min=c(.2, .4, .2, .4),
                     max=c(.4, .7, .4, .7))
  dat <- c("dat1", "dat2")
  labs <- c("Plot 1", "Plot2")
# create plots
  count <- 1
  for (i in dat) {
    p <- ggplot(get(i), aes(x=reorder(as.character(VARIABLES), est), 
                              y=est)) +
    geom_pointrange(aes(ymin=min,
                        ymax=max),
                    linetype="dashed") +
    geom_point(size=3) +
    ylim(-1,1) +
    theme_bw() +
    labs(title = labs[count]) +
    theme(legend.position="none") +
    coord_flip()
    assign(paste(i, "plot", sep="."), p)
    count <- count+1
  }
# combine plots
  library(gridExtra)
  # approach suggested by @baptise
  # http://stackoverflow.com/questions/13294952/left-align-two-graph-edges-ggplot
  gA <- ggplotGrob(dat1.plot)
  gB <- ggplotGrob(dat2.plot)
  maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
  gA$widths[2:5] <- as.list(maxWidth)
  gB$widths[2:5] <- as.list(maxWidth)
  grid.arrange(gA, gB, ncol=1)
like image 307
Eric Green Avatar asked Dec 20 '22 14:12

Eric Green


2 Answers

library(gridExtra)
library(grid)

gb1 <- ggplot_build(dat1.plot)
gb2 <- ggplot_build(dat2.plot)

# work out how many y breaks for each plot
n1 <- length(gb1$layout$panel_params[[1]]$y.labels)
n2 <- length(gb2$layout$panel_params[[1]]$y.labels)

gA <- ggplot_gtable(gb1)
gB <- ggplot_gtable(gb2)

g <- rbind(gA, gB)

# locate the panels in the gtable layout
panels <- g$layout$t[grepl("panel", g$layout$name)]
# assign new (relative) heights to the panels, based on the number of breaks
g$heights[panels] <- unit(c(n1,n2),"null")

grid.newpage()
grid.draw(g)

enter image description here

like image 89
baptiste Avatar answered Apr 30 '23 19:04

baptiste


Here's one approach: fill in extra spaces for shorter labels and use a monospace font.

longest_name <- max(nchar(as.character(dat1$VARIABLES)), nchar(as.character(dat2$VARIABLES)))
fill_in_spaces <- function(v) paste0(paste0(rep(" ", longest_name - nchar(v)), collapse=""), v)
levels(dat1$VARIABLES) <- sapply(levels(dat1$VARIABLES), fill_in_spaces)
levels(dat2$VARIABLES) <- sapply(levels(dat2$VARIABLES), fill_in_spaces)

Then the plotting procedure is almost the same, just add

p <- p + theme(text=element_text(family="Courier", size=14))

enter image description here

There is a minor issue: levels are reordered, so Item 3 is now the last, but that can be easily fixed like described e.g. here.

like image 24
tonytonov Avatar answered Apr 30 '23 19:04

tonytonov