Question

Using the data in this question https://stackoverflow.com/questions/21663108/coloring-points-on-a-lattice-plot-in-r I want to use the histogram function in lattice to plot the three histograms on the same plot but separately so that I can control the colors and allow for a semi-transparent color where the histograms overlap.

However, the following code doesn't work:

histogram(locs[locs.col=="darkblue"] , breaks=20, xlim=c(25,150),col="darkblue" )
histogram(locs[locs.col=="yellow"] , breeaks=20, xlim=c(25,150),col="yellow",add=T ) # add doesn't work here
histogram(locsy[locs.col=="red"] , breaks=20, xlim=c(25,150),col="red",add=T )

because the histograms won't be added to eachother. I know this works for the hist function in base packages so here are my questions:

1) Is there a way of adding histograms to the plot with histogram? 2) How can I make the bins of the histograms to be the same width for each instance of a histogram 3) How can I make the colors semi-transparent for where the histograms overlap? 4) How can I rotate the histogram 90 degrees so that it frequency is the horizontal axis?

Was it helpful?

Solution 2

The lattice histogram function does not support the add=T which is part of base graphics. Furthermore, the usual way to get side-by-side or overlapping plots in lattice is with a 'groups' parameter, and histogram again does not support groups. But the help page says thatdensityplot` will and it also plots the locations of the data points and accepts an alpha-transparency argument:

df <- data.frame(locs=locs, locs.col=locs.col,dataset=dataset)
densityplot(~locs, groups=locs.col,data=df , xlim=c(25,150), alpha=.5 )

enter image description here

If you want your own colors you can try: ...,col=locs.col,...

To add materioal to what started out as a comment about how to "rotate" a density plot:

An example of integrating densities with histogram calls that surprisingly enough I get credit (or blame) for:

http://markmail.org/search/?q=list%3Aorg.r-project.r-help++densityplot+switch+x+y#query:list%3Aorg.r-project.r-help%20%20densityplot%20switch%20x%20y+page:1+mid:oop3shncxgx4mekc+state:results

--------text------ Use densityplot instead of histogram as the wrapping function so its more extreme ranges are respected. You do get an error when you do that saying that 'breaks' is invalid, but if you read the ?histogram page, it suggests that setting breaks=NULL might produce acceptable default behavior, and that seems to be so in this case:

densityplot(~x,data=foo,groups=grp, 
#prepanel=function(x,type,groups,...){???}, 
  panel=function(x,type,groups,...){ 
    panel.densityplot(x,groups=groups,...) 
    panel.histogram(x,col='transparent', breaks = NULL, ...)

} ) 

-------end quoted material-------

And an example of hacking (by Dieter Menne) showing how to splice hacked panels into a lattice call: http://markmail.org/search/?q=list%3Aorg.r-project.r-help++densityplot+switch+x+y#query:list%3Aorg.r-project.r-help%20%20densityplot%20switch%20x%20y+page:1+mid:fyva5hrio6cn4fs2+state:results

OTHER TIPS

Overlapping histograms can be sometimes comprehensible and informative, however, it's usually better to use bwplot or violin plot, anyway:

histogram( ~Sepal.Length,
     data = iris,
     type = "p",
     breaks = seq(4,8,by=0.2),
     ylim = c(0,30),
     groups = Species,
     panel = function(...)panel.superpose(...,panel.groups=panel.histogram,
                          col=c("cyan","magenta","yellow"),alpha=0.4),
     auto.key=list(columns=3,rectangles=FALSE,
                   col=c("cyan","magenta","yellow3"))
     )

overlapping histograms in R with lattice, Sepal.Length from iris data

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top