Question

I have the following dual plot (from another SO question):

dual plot

Here's the code that generates the plot:

library(ggplot2)
library(gtable)

df <- data.frame(x=c(5,2,7,3),
    y=c("asdasxfqwe","a","b","c"),
    facet=c(1,1,2,2))

# First plot (a bit of extra space between facets)
p <- ggplot(df, aes(x, y)) + facet_grid(~facet) + 
        geom_point() + 
        theme(panel.margin = unit(4, "lines"),
              axis.text.y  = element_text( hjust=0.5))

# get y-axis labels 
g <- ggplotGrob(p)
axis <- gtable_filter(g, "axis-l")[["grobs"]][[1]][["children"]][["axis"]][,1]

# remove axis
g[["grobs"]][[4]][["children"]][["axis"]] <- NULL

# build plot & add axis to LHS of left facet
panels <- subset(g$layout, name == "panel")
g <- gtable_add_grob(g, grobs=axis, t = unique(panels$t),
    l=tail(panels$l, -1)-1)

grid.newpage()
grid.draw(g)

As I understand, the empty space on the left is where the y-axis text used to be before it was moved using the gtable code. How to get rid of this empty space?

Était-ce utile?

La solution

Upgraded comment:

Since you're editing the gtable, you can set the relevant width to something smaller,

 g[["widths"]][3] <- list(unit(1, "line"))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top