【问题标题】:How to merge two or more 2D histograms together in R?如何在 R 中将两个或多个 2D 直方图合并在一起?
【发布时间】:2015-09-08 05:22:29
【问题描述】:

如何合并 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()

【问题讨论】:

  • 看起来你需要的是一个变量,表示每个数据点属于两个类别中的哪一个;那么您的填充美学由该因素决定,而 alpha 由计数决定。这至少会在一组轴上获得两个类别。但是导游会有点麻烦。
  • 谢谢。所需要的只是将绿色数据放在蓝色的顶部,将绿色的图例放在图的左侧。绿色不应与蓝色重叠(可能在一些小点),因为它们在时间上是可分离的。

标签: r plot histogram


【解决方案1】:

感谢 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()

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 2015-01-14
    • 1970-01-01
    • 2016-11-23
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多