I appreciate the idea of spark tables, but my limited interaction with
them was to compactly compare time series data in a dashboard. I don't
think they were meant to compare distributions in such a limited
space, but I could be wrong.
One way to get what you want in ggplot2 could be to facet by treatment
and to label the mean effect in each facet. By default, faceting
produces the same x- and y-limits. In the code below, I expand the
x-limits somewhat to provide enough room for the labels.
DF <- data.frame(trt = factor(rep(LETTERS[1:3], each = 100)),
y = rnorm(300, mean = rep(c(0.5, 0.1, -0.1), each = 100),
sd = 0.1))
library(plyr)
library(ggplot2)
fx <- ddply(DF, "trt", summarise, y = round(mean(y), 2))
fx$lab <- paste0("Effect: ", fx$y)
# Treatments as facet labels, effects as annotation
ggplot(DF, aes(x = y)) +
theme_bw() +
geom_density(fill = "blue", color = NA, alpha = 0.5) +
geom_vline(xintercept = 0, color = "grey30") +
labs(x = "Effect", y = "Density") +
xlim(-0.5, 1) +
geom_text(data = fx, aes(x = -0.5, y = 4, label = lab), hjust = 0) +
facet_wrap(~ trt, ncol = 1)
An alternative approach, although not particularly ideal, is to add
the treatments and effects as labels in each density plot. Here's how
it might work as a first draft on the above toy example, although with
more groups you want a more programmatic solution than this.
p1 <- ggplot(subset(DF, trt == "A"), aes(x = y)) +
theme_bw() +
geom_density(fill = "blue", color = NA, alpha = 0.5) +
geom_vline(xintercept = 0, color = "grey30") +
labs(x = NULL, y = "") +
xlim(-1.5, 1) +
geom_text(x = -1.25, y = 4, label = "Treatment") +
geom_text(x = -0.75, y = 4, label = "Effect") +
geom_text(x = -1.25, y = 2, label = "A") +
geom_text(x = -0.75, y = 2, label = fx[1, "y"]) +
theme(axis.text.x = element_blank())
p2 <- ggplot(subset(DF, trt == "B"), aes(x = y)) +
theme_bw() +
geom_density(fill = "blue", color = NA, alpha = 0.5) +
geom_vline(xintercept = 0, color = "grey30") +
labs(x = NULL, y = "") +
xlim(-1.5, 1) +
geom_text(x = -1.25, y = 2, label = "B") +
geom_text(x = -0.75, y = 2, label = fx[2, "y"]) +
theme(axis.text.x = element_blank())
p3 <- ggplot(subset(DF, trt == "C"), aes(x = y)) +
theme_bw() +
geom_density(fill = "blue", color = NA, alpha = 0.5) +
geom_vline(xintercept = 0, color = "grey30") +
labs(x = "Effect", y = "") +
xlim(-1.5, 1) +
geom_text(x = -1.25, y = 2, label = "C") +
geom_text(x = -0.75, y = 2, label = fx[3, "y"])
library(gridExtra)
grid.arrange(p1, p2, p3, ncol = 1)
A third approach would be to create a gridTable of the appropriate
size for the treatments and effect along with the initial ggplot (sans
the annotation), each saved to an object. You might then do something
like
grid.arrange(my_grid_table, my_faceted_ggplot, nrow = 1)
with appropriate sizing in the x-direction, to generate a side-by-side
table with corresponding densities. See ?grid.arrange for further
details.
If any of these options appeal to you (and they may not), you'll still
need to fiddle with the size and type of graphic to export to your
Word document. This example is simply to illustrate different
possibilities with ggplot2. Your real example may contain far more
levels, enough so to make these illustrations impractical for your use
case. In my example, I also took the label positions from a
preliminary view of the graph; if you want a more programmatic
solution, you'll have to DIY.
Dennis