简体   繁体   中英

Using Factor Variables to Facet a Histogram Beside a Scatter Plot in R

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 (eg, the scatterplot data is 2D, the histogram data are 1D).

I am not sure if I fully understand the question since a histogram of factor variables doesn't quite make sense to me. Also, without sample data, I will just have to use mtcars . Something like this might help. I use grid.extra in addition to ggplot2 in order to make the plots have a custom grid arrangement.

library(gridExtra)
library(ggplot2)


s_plot <- ggplot(data = mtcars, aes(x = hp, y = mpg)) + geom_point()

h1 <- ggplot(data = mtcars, aes(x = hp)) + geom_histogram()

h2 <- ggplot(data = mtcars, aes(x = mpg)) + geom_histogram()

grid.arrange(s_plot, h1, h2, layout_matrix = cbind(c(1, 1), c(2, 3)))

在此处输入图片说明

Note that in the layout_matrix argument in grid.arrange , I use cbind(c(1,1), c(2, 3)) because I want the first plot to be in a column all by itself and then I want the other two plots to occupy individual rows in the second column of the grid.

Consider the use of geom_rug .

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() + geom_rug()

在此处输入图片说明

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")
)

具有数据x和y分量直方图的散点图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM