skip to Main Content

I want to put three histogram plots (using this code) into single plot:

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

and just the maian plot x axis is scaled. i tried to provide the idea picture by photoshop.

idea picture

Could anyone help me?

Edit:
Sorry for delay and Thank you for answers.I tried suggested options.

preparing data and functions

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

plot by phoxis suggested option:

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)

The out put file would be like:
enter image description here

and if i use 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)

The output would be something like this:
enter image description here

I also tried user1317221_G suggestion with modification:

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

And the output:
enter image description here

Conclusion: user1317221_G’s suggestion is more like putting three histogram plots into another plot.
I still seek method to put three histogram plots into single plot by base plot option and not by tricky ways like closing three separate plots to each other and removing borders and etc..

2

Answers


  1. How about

    par (mfrow=c(3,1))

    And then call your plot three times?

    The mfrow describes 3 rows and 1 column (as you have shown in the picture). You can arrange the multiple plots as you wish. A plot call will plot the graph on the next sub-plot. If you want to add points in a specific sub-plot, use lines or points as usual.

    Login or Signup to reply.
  2. Here is a quick way with ggplot2, the labelling of the axes is a bit dirty but does the job without invoking gtable and grob etc.

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search