单个图框中的多个直方图

时间:2016-11-04 14:44:43

标签: r plot

我想将三个直方图(using this code)放入单个图中:

plot(0, type="n", xlab="", ylab="", xlim=c(-10, 10))
abline(v=0)

只是缩放了maian图x轴。我试图通过photoshop提供想法图片。

idea picture

有人可以帮助我吗?

编辑: 抱歉延迟,谢谢你的回答。我试过建议的选项。

准备数据和功能

source("http://www.indiana.edu/~kruschke/DoingBayesianDataAnalysis/Programs/plotPostOLD.R")
download.file("http://www.indiana.edu/~kruschke/DoingBayesianDataAnalysis/Programs/HDIofMCMC.R",destfile = "HDIofMCMC.R")
source("HDIofMCMC.R")
library(fGarch)
library(Runuran)
norm<-rnorm(20000)
snorm<-rsnorm(20000)
repeat{
laplas<-urlaplace(20000, location=0, scale=1, lb = -2.5, ub = 2.5)
if(abs(mean(laplas))<.002 & sd(laplas)<1.05 & sd(laplas)>.95) break()}

通过phoxis建议选项:

dev.off()
par (mfrow=c(3,1))
plotPost(norm,xlim = c(-6, 6),xaxt='n')
par(new)
abline(v=0)
plotPost(snorm,xlim = c(-6, 6),xaxt='n')
par(new)
abline(v=0)
plotPost(laplas,xlim = c(-6, 6))
par(new)
abline(v=0)

输出文件如下: enter image description here

如果我使用split.screen():

split.screen(rbind(c(0.1, 0.98,0.1,0.33), c( 0.1, 0.98,0.33, 0.66),c(0.1, 0.98,0.66,0.95)))
screen(1)
par(mar = c(0, 0, 0, 0))
plotPost(norm,xlim = c(-6, 6))
abline(v=0)
screen(2)
par(mar = c(0, 0, 0, 0))
plotPost(laplas,xlim = c(-6, 6),xaxt='n')
abline(v=0)
screen(3)
par(mar = c(0, 0, 0, 0))
plotPost(snorm,xlim = c(-6, 6),xaxt='n')
abline(v=0)
close.screen(all.screens = TRUE)

输出将是这样的: enter image description here

我还尝试了修改后的用户1317221_G建议:

deeta <- data.frame( value = c(laplas,snorm,norm),
                     HDI1=c(rep(HDIofMCMC(laplas , .95 )[[1]],2000),rep(HDIofMCMC(norm, .95 )[[1]],2000),rep(HDIofMCMC(snorm, .95 )[[1]],2000)),
                     HDI2=c(rep(HDIofMCMC(laplas , .95 )[[2]],2000),rep(HDIofMCMC(norm, .95 )[[2]],2000),rep(HDIofMCMC(snorm, .95 )[[2]],2000)),

type = c(rep("skewed",20000),rep("laplas",20000),
         rep("normal",20000)))
library(ggplot2)
library(grid)
gg <-  ggplot(deeta, aes(x=value)) +
  geom_histogram(fill = "skyblue",col="white",bins = 50) +
  geom_segment(data=deeta,aes(x=HDI1,xend=HDI2,y=.3,yend=.3),size=1.5)+
  geom_vline(xintercept = 0) +
  facet_grid(type ~.) +        
  theme(
        panel.margin = unit(-0, "lines"),
        strip.background = element_rect(fill = "white"),panel.grid = element_blank(),panel.background = element_blank())

输出: enter image description here

结论:user1317221_G的建议更像是将三个直方图绘制到另一个图中。 我仍然寻求方法将三个直方图绘制成单个绘图的基础绘图选项,而不是通过巧妙的方式,如关闭三个单独的绘图并删除边框等。

2 个答案:

答案 0 :(得分:1)

怎么样

par (mfrow=c(3,1))

然后三次打电话给你的情节?

mfrow描述了3行1列(如图所示)。您可以根据需要安排多个地块。 plot调用将在下一个子图上绘制图形。如果您想在特定子图中添加点,请照常使用linespoints

答案 1 :(得分:1)

以下是使用ggplot2的快速方法,轴的标签有点脏,但无需调用gtablegrob等工作。

deeta <- data.frame( value = c(sample(10,100, replace = TRUE),
                               sample(20,100, replace = TRUE),
                               sample(5,100, replace = TRUE)
                               ),
                     type = c(rep("skewed",100),rep("laplas",100),
                             rep("normal",100))
                    )
library(ggplot2)
library(grid)

gg <-  ggplot(deeta, aes(x=value)) +
 geom_histogram(fill = "lightblue") +
 geom_vline(xintercept = 10) +
 facet_grid(type ~.) +        
 theme(strip.text = element_text(size = rel(3.0), vjust = -4.0),
               panel.margin = unit(-1, "lines"),
               strip.background = element_rect(fill = "transparent")) +
      ylab(paste("normal","laplas","skewed",
           sep = "                          " ))

enter image description here