【问题标题】:Align discrete and continuous axes with ggplot2 and grid将离散轴和连续轴与 ggplot2 和网格对齐
【发布时间】:2015-07-08 17:39:55
【问题描述】:

我正在尝试显示几个变量的汇总每周数据的网格图。该图最相关的两个组成部分是某个变量在给定一周内占用的值的分布汇总图(即箱线图或小提琴图)和一个整数变量在数周内累积的累积计数图(因此阶梯图)。我想使用grid 在对齐的 x 轴上绘制这两个图。我将使用ggplot2 制作单独的图表,因为我迷恋 Hadley Wickham(j/k,ggplot 真的非常非常好)。

问题是geom_boxplot 只为 x 轴取因子,geom_step 只为 x 轴取连续数据。即使您使用 coord_cartesianscale_x_... 强制类似的 x 限制,这些也不一定对齐。

我已经使用 geom_rect 拼凑了一个 hack,它适用于这个特定的应用程序,但是如果我有一些其他因素导致一周内出现多个盒子,那么这将是一个痛苦的调整。

强制性的可重现性:

library(ggplot2)
library(grid)

var1 <- data.frame(val = rnorm(300),
                   week = c(rep(25, 100), 
                        rep(26, 100), 
                        rep(27, 100))
                  )

var2 <- data.frame(cumul = cumsum(c(0, rpois(2, 15))),
                   week = c(25, 26, 27)
                  )


g1   <- ggplot(var1, aes(x = factor(week), y = val)) + 
  geom_boxplot()

g2   <- ggplot(var2, aes(x = week, y = cumul)) + 
  geom_step() + scale_x_continuous(breaks = 25:27)

grid.newpage()
grid.draw(rbind(ggplotGrob(g1),
                ggplotGrob(g2),
                size = "last"))

还有杂牌:

library(dplyr)

chiggity_check <- var1 %>% 
  group_by(week) %>% 
  summarise(week.avg = mean(val),
            week.25  = quantile(val)[2],
            week.75  = quantile(val)[4],
            week.05  = quantile(val)[1],
            week.95  = quantile(val)[5])

riggity_rect <- ggplot(chiggity_check) +
  geom_rect(aes(xmin = week - 0.25, xmax = week + 0.25,
                ymin = week.25,
                ymax = week.75)) +
  geom_segment(aes(x = week - 0.25, xend = week + 0.25,
                   y = week.avg, yend=week.avg),
               color = "white") +
  geom_segment(aes(x = week, xend = week ,
                   y = week.25, yend=week.05)) +
  geom_segment(aes(x = week, xend = week ,
                   y = week.75, yend=week.95)) +
  coord_cartesian(c(24.5,27.5)) +
  scale_x_continuous(breaks = 25:27)

grid.newpage()
grid.draw(rbind(ggplotGrob(riggity_rect),
                ggplotGrob(g2 + coord_cartesian(c(24.5,27.5))),
                size = "last"))

所以问题是:有没有办法强制geom_boxplot 到连续轴或geom_step 到因子轴?还是有其他一些实现,也许stat_summary 会更灵活一点,这样我就可以对齐轴并可能很容易地添加诸如分组颜色变量之类的东西?

【问题讨论】:

  • 为了更容易破解,您可以指定轴限制。例如,将 limits = c(24.55, 27.45) 添加到连续刻度似乎适用于您的示例。
  • 是的,但是如果周数发生变化,偏移量可能也会发生变化。

标签: r plot ggplot2 r-grid


【解决方案1】:

一种方法是在使用factor(week) 设置的 x 轴上绘制两个图表,但在 g2 图(阶梯图)中,在geom_blank() 中这样做,以便设置比例。然后在geom_step() 中,以数字比例绘制:as.numeric(factor(week))

library(ggplot2)
library(grid)

# Your data
var1 <- data.frame(val = rnorm(300),
                   week = c(rep(25, 100), 
                        rep(26, 100), 
                        rep(27, 100))
                  )

var2 <- data.frame(cumul = cumsum(c(0, rpois(2, 15))),
                   week = c(25, 26, 27)
                  )

# Your g1
g1   <- ggplot(var1, aes(x = factor(week), y = val)) + 
  geom_boxplot()

# Modified g2
g2   <- ggplot(var2) + geom_blank(aes(x = factor(week), y = cumul)) +
geom_step(aes(x = as.numeric(as.factor(week)), y = cumul)) 

grid.newpage()
grid.draw(gridExtra::rbind.gtable(ggplotGrob(g1),
                ggplotGrob(g2),
                size = "last"))

【讨论】:

  • 挖吧!像魅力一样工作。
猜你喜欢
  • 1970-01-01
  • 2016-05-10
  • 2018-01-03
  • 2021-05-03
  • 2021-11-27
  • 2017-03-01
  • 2016-09-26
  • 2021-12-04
相关资源
最近更新 更多