简体   繁体   English

使用因子变量在R中的散点图旁边划分直方图

[英]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)? 是否可以使用因子变量在R中的ggplot2中的散点图下方或旁边刻划直方图(以使直方图是数据的x和y分量)?

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). 到目前为止,我还无法使它起作用,因为似乎所有分面数据都必须具有相同数量的维度(例如,散点图数据为2D,直方图数据为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 . 另外,如果没有样本数据,我将只需要使用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. 我用grid.extra除了ggplot2为了使该地块有一个自定义网格排列。

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. 需要注意的是,在layout_matrix的说法grid.arrange ,我用cbind(c(1,1), c(2, 3))因为我想第一个情节是全部由自己一列,然后我想其他两个绘制以占据网格第二列中的各个行。

Consider the use of geom_rug . 考虑使用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分量直方图的散点图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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