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?
To make multiple overlapping histograms, we need to use Matplotlib pyplot's hist function multiple times. For example, to make a plot with two histograms, we need to use pyplot's hist() function two times. Here we adjust the transparency with alpha parameter and specify a label for each variable.
Plot two histograms If you have a histogram object, all the data you need is contained in that object. Using plot() will simply plot the histogram as if you'd typed hist() from the start. However, you can now use add = TRUE as a parameter, which allows a second histogram to be plotted on the same chart/axis.
An Overlay Histogram allows you to visualize and compare multiple Populations superimposed on each other. The following screenshot shows a simple Overlay plot of Singlets, NK cells and All Events. You can add Populations to the Overlay by dragging them from Populations in the sidebar.
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"))
)
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 that
densityplot` 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 )
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With