overlapping histograms - R

finally! after 2 days the result:
seemingly all go for ggplot functions.... even though i was a bit reluctant to use this function, tried for the past two days adjusting the df in order to apply ggplot... without success (yet ;-) )
however, i stumbled upon code for a lovely little pseudo graph at Chrismiller's blog (chrisamiller.com$BioBits) 
THANKS Chris!!

#i used for DNAmeth/comb.pair:
#at first to simply overlap a and b in hist:
a <- comb.pair="">
b <- comb.pair="">
hist( a, breaks= 40, col= "grey20" )
hist( b, breaks= 40, add= T ) #hahaha here is the trick: add= T!!



wtih a couple of other tricks on the same theme

#adapted for DNAmeth$comb.pair it looks like this:
plotOverlappingHist <- a="" b="" colors="c(" function="" gray20="" gray50="" span="" white="">
                                breaks=40, xlim=NULL, ylim=NULL){

  ahist=NULL
  bhist=NULL
  
  if(!(is.null(breaks))){
    ahist=hist(a,breaks=breaks,plot=F)
    bhist=hist(b,breaks=breaks,plot=F)
  } else {
    ahist=hist(a,plot=F)
    bhist=hist(b,plot=F)

    dist = ahist$breaks[2]-ahist$breaks[1]
    breaks = seq(min(ahist$breaks,bhist$breaks),max(ahist$breaks,bhist$breaks),dist)

    ahist=hist(a,breaks=breaks,plot=F)
    bhist=hist(b,breaks=breaks,plot=F)
  }

  if(is.null(xlim)){
    xlim = c(min(ahist$breaks,bhist$breaks),max(ahist$breaks,bhist$breaks))   
  }

  if(is.null(ylim)){
    ylim = c(0,max(ahist$counts,bhist$counts))   
  }

  overlap = ahist
  for(i in 1:length(overlap$counts)){  
    if(ahist$counts[i] > 0 & bhist$counts[i] > 0){
      overlap$counts[i] = min(ahist$counts[i],bhist$counts[i])
    } else {
      overlap$counts[i] = 0
    }
  }

  plot(ahist, xlim=xlim, ylim=ylim, col=colors[1])
  plot(bhist, xlim=xlim, ylim=ylim, col=colors[2], add=T)
  plot(overlap, xlim=xlim, ylim=ylim, col=colors[3], add=T) 
}
# and then he plots:
plotOverlappingHist(a,b)

#alternatively, someone suggested to use transparency.... riiight here we have the real solution and can be adapted for BGW or COL depending on publication preferences :-)
hist( am, col= rgb( 0, 1, 0,0.5 ), breaks= 40, main= Histogram of frequencies in Monocytes  )
# sit legend in: legend( "topleft", legend... hist( bm, col= rgb( 1, 0, 0,0.5 ), breaks= 40, add=T )

#bellissimo!!

#cell specific:
par( mfrow= c(1,2 ) )
a <- comb.pair="" font="">
am <- a="" font="" grep="" rownames="">
amhhc <- all="" am="" contain="" controls="" for="" from="" hhc:="" household="" m="" span="">
amtb <- active="" all="" am="" cases="" contain="" for="" from="" m="" span="" tb:="" tb="">

hist( amhhc, col= rgb( 0, 1, 0,0.5 ), breaks= 40, main= Histogram of frequencies in Monocytes  )
hist( amtb, col= rgb( 1, 0, 0,0.5 ), breaks= 40, add=T )

an <- a="" font="" grep="" rownames="">
anhhc <- all="" an="" contain="" controls="" for="" from="" hhc:="" household="" n="" span="">
antb <- active="" all="" an="" cases="" contain="" for="" from="" n="" span="" tb:="" tb="">

hist( anhhc, col= rgb( 0, 1, 0,0.5 ), breaks= 40, main= "Histogram of frequencies in Neutrophils")
hist( antb, col= rgb( 1, 0, 0,0.5 ), breaks= 40, add=T )

***************************************
op die ouend wat goed gewerk het op een figuur:
--------------
par( mfrow = c( 1,2 ) )
hist( amhhc, col= rgb( 0,0,0,0.5 ), breaks= 60, main= "Histogram of frequencies in Monocytes", xlab="" )
hist( amtb, col= rgb( 1,1,1,0.5 ), breaks= 60, add= T, main= "", xlab= "" )
hist( anhhc, col= rgb( 0,0,0,0.5 ), breaks= 60, main= "Histogram of frequencies in Neutrophils", xlab="" )

hist( antb, col= rgb( 1,1,1,0.5 ), breaks= 60, add= T, main= "", xlab= "" )
----------------



and then of course the entertainment - ooohhh how we know these!!

Comments

Popular Posts