Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

r - How to add overlapping histograms with lattice

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...