【问题标题】:ggplot: reduce the space reserved for y-axis textggplot:减少为y轴文本保留的空间
【发布时间】:2014-04-27 14:02:48
【问题描述】:

我有以下双重情节(来自another SO question):

这是生成绘图的代码:

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)

据我了解,左侧的空白区域是使用 gtable 代码移动 y 轴文本之前的位置。如何摆脱这个空白?

【问题讨论】:

    标签: r ggplot2 gtable


    【解决方案1】:

    升级评论:

    由于您正在编辑gtable,您可以将相关宽度设置为更小,

     g[["widths"]][3] <- list(unit(1, "line"))
    

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2017-12-05
      相关资源
      最近更新 更多