【问题标题】:ggplot2: Varying facet width with independent `Y` axesggplot2:使用独立的“Y”轴改变刻面宽度
【发布时间】:2019-04-16 20:37:28
【问题描述】:

虚拟数据

d = data.frame(
    x = factor(LETTERS[c(1,2,3,4,1,2,3,4,1,2,1,2,1,2,1,2)]),
    y = c(100,80,70,60,130,90,65,60,2,3,3,3,2,2,1,2),
    grid = rep(letters[1:2], each=8)
)

问题

ggplot(d, aes(x=x, y=y)) + facet_grid(~grid, scales="free",space="free_x") + geom_point()

我喜欢这个图表。我唯一的问题是两个网格都使用相同的Y 轴。所以,我尝试使用facet_wrap 而不是facet_grid 并得到了

ggplot(d, aes(x=x, y=y)) + facet_wrap(~grid, scales="free") + geom_point()

但不幸的是,facet_wrap 没有“空格”参数,因此左右图的宽度相同。

问题

我怎样才能使变量d$x 的级别之间的空间在两个方面之间相等(导致方面具有不同的宽度)并且每个方面都有一个单独的Y 轴。当然,我想保持刻面水平对齐。

【问题讨论】:

标签: r ggplot2 plot gridview graph


【解决方案1】:

使用ggplot grob并修改表格中的宽度

# Capture the plot
q = ggplot(d, aes(x=x, y=y)) + facet_grid(~grid, scales="free",space="free_x") + geom_point()
gt = ggplotGrob(q)

# Modify the widths
gt$widths[5] = unit(8, "cm")
gt$widths[9] = unit(4, "cm")

# Plot the graph
grid.newpage()
grid.draw(gt)

【讨论】:

  • 对于指定刻面宽度的较少手动方式,您可以参考facet_grid 版本的绘图并将相对宽度应用于facet_wrap 版本。我之前对平面高度 herehere 做过类似的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 2022-11-18
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
相关资源
最近更新 更多