2015-06-22 118 views
1

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

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

+0

看起来你需要的是一个变量代表什么这两个类别的每个数据点落入的;那么你的填充美学就是由这个因素决定的,而alpha则由数字决定。这至少会在一组轴上得到两个类别。但导游会有点麻烦。 – tegancp

+0

谢谢。所有需要的是将绿色数据放在蓝色的顶部,将绿色的图例放在图的左侧。绿色不应该与蓝色重叠(可能在一些小点),因为它们可以及时分开。 – Dimon

回答

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

enter image description here