I am trying to display a grid shape of summed weekly data from several variables. The two components of this graph that are most relevant are a summary graph of the distribution (so that the graph or the violin graph) of the values that some variables took during the week, and the cumulative graph of the integer variable graph that accumulates over the weeks (so that is the step). I would like to plot these two graphs on a aligned x axis using grid. I will use it ggplot2to create separate graphs, because I heard Hadley Wickham (j / k, ggplot - it really is really, really nice).
The problem is that geom_boxplotit only accepts factors for the x axis, and geom_stepaccepts continuous data only for the x axis. They are not necessarily aligned, even if you apply the same x-limits with coord_cartesianor scale_x_....
I combined the hack with the help geom_rectthat will work for this particular application, but it will be a pain to adapt if, for example, I have another factor that leads to the appearance of several mailboxes within one week.
Mandatory reproducible:
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"))

And kludge:
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, , , , , , ?