如何使用晶格添加重叠的直方图 [英] How to add overlapping histograms with lattice

查看:94
本文介绍了如何使用晶格添加重叠的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此问题中的数据 https://stackoverflow.com /questions/21663108/coloring-points-on-a-lattice-plot-in-r 我想使用lattice中的histogram函数在同一图上绘制三个直方图,但要分别绘制,以便我可以控制颜色,并允许直方图重叠的半透明颜色.

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 )

因为直方图不会相互添加.我知道这适用于基本软件包中的hist函数,所以这是我的问题:

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)是否可以使用histogram向图中添加直方图? 2)如何使每个直方图实例的直方图箱宽度相同 3)如何使直方图重叠的地方的颜色半透明? 4)如何将直方图旋转90度,使其频率成为水平轴?

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?

推荐答案

点阵histogram函数不支持作为基本图形一部分的add = T.此外,获取并排或重叠点阵图的常用方法是使用"groups"参数,并且histogram again does not support groups. But the help page says that densityplot`还将绘制数据点的位置并接受alpha-transparency参数:

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 )

如果您想要自己的颜色,可以尝试:...,col=locs.col,...

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:

--------文本------ 使用密度图代替直方图作为包装函数,因此应遵守其更极端的范围.说"breaks"无效时,您会得到一个错误,但如果阅读?histogram页面,则表明将breaks = NULL设置可能会产生可接受的默认行为,在这种情况下,情况似乎是这样的: /p>

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

还有一个黑客实例(由Dieter Menne撰写),展示了如何将被黑客入侵的面板拼接成格子状调用:

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

这篇关于如何使用晶格添加重叠的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆