如何在 R 中将两个或多个 2D 直方图合并在一起?

How to merge two or more 2D histograms together in R?

如何合并 2 个直方图 qplots 并将它们放在同一个图上而不是 2 个单独的图上?关于简单的一维直方图有一个类似的问题:How to plot two histograms together in R? 但是,他们不使用 qplots,而是使用简单的一维直方图。 下面是原始代码。如果您可以修改它,让每个直方图图例一个在左侧,另一个在图的右侧,那将让我开心。非常感谢!!!

  plt_d = d[s & grepl("Rd", d$Cmd), c("Time_Stamp_Norm",
    "Virtual_Address_Norm")]

  p1 <- qplot(Time_Stamp_Norm, Virtual_Address_Norm, data=plt_d,
    geom='bin2d', binwidth = hist_binwidth,
    xlab="Normalized Time",
    ylab="Normalized Address",
    main="Read requests in virtual address space") +
    scale_fill_gradient(low="#C6DBEF", high="#08306B") +
    xlim(0, 1+b_inv) + ylim(0, 1+b_inv) +
    theme(axis.text=element_text(size=10), axis.title=element_text(size=10),
    plot.title=element_text(size=10))

  plt_d = d[s & grepl("Wr", d$Cmd), c("Time_Stamp_Norm",
    "Virtual_Address_Norm")]

  p2 <- qplot(Time_Stamp_Norm, Virtual_Address_Norm, data=plt_d,
    geom='bin2d', binwidth = hist_binwidth,
    xlab="Normalized Time",
    ylab="Normalized Address",
    main="Write requests in virtual address space") +
    scale_fill_gradient(low="#C7E9C0", high="#00441B") +
    xlim(0, 1+b_inv) + ylim(0, 1+b_inv) +
    theme(axis.text=element_text(size=10), axis.title=element_text(size=10),
    plot.title=element_text(size=10))

  ...

  print(p1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
  print(p2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))

  ...

  dev.off()

感谢 tegancp 的建议。这是我的解决方案:

x <- c(rnorm(n=1000, mean=5, sd=1), rnorm(n=1000, mean=7, sd=1))
y <- c(rnorm(n=1000, mean=5, sd=1), rnorm(n=1000, mean=7, sd=1))
d <- data.frame(x,y)
d$type=c(rep("Rd",1000),rep("Wr",1000))
p <- ggplot(d) + geom_bin2d(aes(x=x, y=y, alpha=..count.., fill = d$type))
pdf(file="my_file.pdf")
print(p)
dev.off()