1

How would it be possible to merge 2 histogram qplots and put them on the same plot instead of 2 separate plots? There is a similar question about simple 1D histograms: How to plot two histograms together in R? However, they don't use qplots and they use simple 1D histograms. Below is the original code. If you could modify it to have each of histogram legends to be one on left side and another one on the right side of the plot that would make my day. Thank you so much!!!

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

enter image description here

Community
  • 1
  • 1
Dimon
  • 436
  • 5
  • 15
  • Looks like what you need is a variable representing which of the two categories each data point falls into; then your fill aesthetic is determined by that factor, and alpha by the count. That would at least get both categories on one set of axes. But the guides would be a bit troublesome. – tegancp Jun 22 '15 at 19:15
  • Thank you. All is needed is to put the green data on the top of blue and the legend from the green to put on the left hand side of the plot. The green should not overlap the blue (maybe at some minor spots) because they are separable in time. – Dimon Jun 22 '15 at 19:48

1 Answers1

1

Thanks to tegancp for advise. Here is my solution:

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

enter image description here

Dimon
  • 436
  • 5
  • 15