r - 使用网格对齐 ggplot2 轴

标签 r ggplot2

我有一个带有奇数个面的 ggplot2 线图。我想添加空白区域中 x 值的边际分布的箱线图。箱线图应该是水平的,并与其他方面共享一个共同的 x 轴。因为默认的箱线图几何是垂直的,所以需要 coord_flip() 。因此,我认为不可能使用虚拟因子变量进行分面,将箱线图数据包含在与其他分面相同的 df 中。

使用grid,我可以识别空视口(viewport)并插入箱线图,但我想让 x 轴对齐。类似问题的答案(请参阅 herehere )建议使用 ggExtra 包中的 align_plots ,但我认为这不适用于分面。我在下面提供了一个简单的可重现示例。如果我能做到这一点,我还必须编辑空面板 Grob 以创建与其他方面匹配的新标签。如有任何建议,我们将不胜感激。

library(ggplot2)
#generate df for faceted line graphs
df <- data.frame(x = rep(1:100, times=7),
        facet_var = as.factor(rep( 1:7, each=100)),
        y = runif(7*100)
      ) 
#create faceted line graphs
p <- ggplot(data = df, aes(x, y)) +
    geom_line() + facet_wrap( ~ facet_var, ncol=2)

#generate df for boxplot
xdata <- runif(1000, min = 0, max = 100)
boxdf <- data.frame(x=xdata, group=rep(1,length(xdata)))

#create boxplot removing axes and margins
q <- ggplot(data = boxdf, aes(as.factor(group),x)) + geom_boxplot() +
       coord_flip() + labs(x=NULL) +
       opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(),
         axis.text.y = theme_blank(), axis.title.y=theme_blank(),
         axis.ticks = theme_segment(colour = "white"),
         panel.margin = 0, plot.margin = unit(rep(0,4), "lines")
       )

print(p)
downViewport("panel-14-5")
print(q, newpage=F)

编辑:在 kohske 的有用回答之后,我尝试根据不同的 x 限制和中断调整代码。下面的代码仅将 x 限制和中断更改为 (0,80) 范围。可能我在代码中遗漏了一些需要根据限制进行更改的内容。

library(ggplot2)
df <- data.frame(x = rep(1:80, times=7),
        facet_var = as.factor(rep( 1:7, each=80)),
        y = runif(7*80)
      ) 

# label for marginal plot
df <- rbind(df, data.frame(x = NA, y = NA, facet_var = "Boxplot wow"))

p <- ggplot(data = df, aes(x, y)) +
    geom_line() + facet_wrap( ~ facet_var, ncol=2) + 
    # set limits for adjustment
    coord_cartesian(xlim = c(0, 80)) +
    #scale_x_continuous(breaks = 1:4*20)
    opts()

xdata <- runif(1000, min = 0, max = 80)
boxdf <- data.frame(x=xdata, group=rep(1,length(xdata)))

q <- ggplot(data = boxdf, aes(as.factor(group),x)) + geom_boxplot() +

    # set breaks and limits for adjustment
    coord_flip(ylim = c(0, 80)) + labs(x=NULL) + 
    scale_y_continuous(breaks = 1:4*20) + 

    # opts for full region drawing: 
    # see https://kohske.wordpress.com/2010/12/25/drawing-on-full-region-in-ggplot2/
    opts(
    legend.position = "none",
    panel.margin = unit(0,"null"),
    plot.margin = rep(unit(0,"null"),4),
    axis.ticks = theme_blank(),
    axis.text.x = theme_blank(),
    axis.text.y = theme_blank(),
    axis.title.x = theme_blank(),
    axis.title.y = theme_blank(),
    axis.ticks.length = unit(0,"null"),
    axis.ticks.margin = unit(0,"null")
  )

print(p)

# remove unused panel
grid.remove("panel-14-5")

downViewport("panel-14-5")
print(q, newpage=F)

enter image description here

最佳答案

这是一个有点肮脏的黑客:

library(ggplot2)
df <- data.frame(x = rep(1:100, times=7),
        facet_var = as.factor(rep( 1:7, each=100)),
        y = runif(7*100)
      ) 

# label for marginal plot
df <- rbind(df, data.frame(x = NA, y = NA, facet_var = "Boxplot wow"))

p <- ggplot(data = df, aes(x, y)) +
    geom_line() + facet_wrap( ~ facet_var, ncol=2) + 
    # set limits for adjustment
    coord_cartesian(xlim = c(0, 100))

xdata <- runif(1000, min = 20, max = 80)
boxdf <- data.frame(x=xdata, group=rep(1,length(xdata)))

q <- ggplot(data = boxdf, aes(as.factor(group),x)) + geom_boxplot() +

    # set breaks and limits for adjustment
    coord_flip(ylim = c(0, 100)) + labs(x=NULL) + 
    scale_y_continuous(breaks = 1:5*20) + 

    # opts for full region drawing: 
    # see https://kohske.wordpress.com/2010/12/25/drawing-on-full-region-in-ggplot2/
    opts(
    legend.position = "none",
    panel.margin = unit(0,"null"),
    plot.margin = rep(unit(0,"null"),4),
    axis.ticks = theme_blank(),
    axis.text.x = theme_blank(),
    axis.text.y = theme_blank(),
    axis.title.x = theme_blank(),
    axis.title.y = theme_blank(),
    axis.ticks.length = unit(0,"null"),
    axis.ticks.margin = unit(0,"null")
  )

print(p)

# remove unused panel
grid.remove("panel-14-5")

downViewport("panel-14-5")
print(q, newpage=F)

enter image description here

关于r - 使用网格对齐 ggplot2 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7410525/

相关文章:

r - 动态库未在 R 二进制包构建中加载

r - 如何在iGraph中为不同顶点设置不同的透明度

r - 根据额外因素用多边形边框覆盖 ggplot 分组图 block

许多数据类的 R 调色板

r - ggplot - facet wrap - 调整比例以显示值之间的明显差异

r - ggplot2中直方图的颜色

javascript - 使用回调选项选择新页面后,R DT 数据表不保留行索引/计数器列

r - R中对长数的操作

r - 将观察数量添加到多个 ggplot2 箱线图中

r - 如何获得共享至少 4 列的公共(public)组的最大行集?