Is it possible to use a factor variable to facet a histogram below or beside a scatter plot in ggplot2 in R (such that the histograms are of the x- and y-components of the data)?
The reason I ask whether this could be done with a factor variable is because faceting seems to be more general than the available packages that address this issue, where, for example with faceting, facet labels can be turned on or off, and also faceting has a more standard appearance where publication may be a concern. (Faceting also by default preserves the use of the same axes).
So far I haven't been able to get this to work because it seems like all faceted data have to be of the same number of dimensions (e.g., the scatterplot data is 2D, the histogram data are 1D).
Nick and Brian,
Thanks for your help with the code. I asked around and was able to get the set-up I was looking for. Basically it goes like this, as shown below. (Hopefully this might be useful to you and others in the future, as I think this is a common type of graph):
rm(list = ls())
library(ggplot2)
library(gridExtra)
df <- data.frame(
x = rnorm(100),
y = rnorm(100)
)
xrange <- range(pretty(df$x))
yrange <- range(pretty(df$y))
p.left <- ggplot(df, aes(y)) +
geom_histogram() +
lims(x = yrange) +
coord_flip() +
theme_light() +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
panel.grid.major.x = element_blank(),
plot.margin = unit(c(1, 0.05, 0.05, 1), "lines")
)
p.blank <- ggplot() +
theme_void() +
theme(plot.margin = unit(rep(0, 4), "lines"))
p.main <- ggplot(df, aes(x, y)) +
geom_point() +
lims(x = xrange, y = yrange) +
theme_light() +
theme(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.margin = unit(c(1, 1, 0.05, 0.05), "lines")
)
p.bottom <- ggplot(df, aes(x)) +
geom_histogram() +
lims(x = xrange) +
theme_light() +
theme(
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major.y = element_blank(),
plot.margin = unit(c(0.05, 1, 1, 0.05), "lines")
)
lm <- matrix(1:4, nrow = 2)
grid.arrange(
p.left, p.blank, p.main, p.bottom,
layout_matrix = lm,
widths = c(1, 5),
heights = c(5, 1),
padding = unit(0.1, "line")
)