如何在每个面板上叠加直方图 [英] How to superimpose a histogram on each panel

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

问题描述

我想在每个晶格直方图面板上叠加一个附加的直方图(每个面板上的直方图都相同).我希望覆盖的直方图具有实线边框,但要填充空白(col),以便与基础直方图进行比较.

I would like to superimpose, on each lattice histogram panel, an additional histogram (which will be the same in each panel). I want the overlayed histogram to have solid borders but empty fill (col), to allow comparison with the underlying histograms.

也就是说,最终结果将是一系列面板,每个面板具有不同的彩色直方图,并且每个面板在彩色直方图的顶部均具有相同的额外轮廓直方图.

That is, the end result will be a series of panels, each with a different colored histogram, and each with the same extra outline histogram on top of the colored histogram.

这是我尝试过的方法,但它只会产生空白面板:

Here's something that I tried, but it just produces empty panels:

foo.df <- data.frame(x=rnorm(40), categ=c(rep("A", 20), rep("B", 20)))
bar.df <- data.frame(x=rnorm(20))
histogram(~ x | categ, data=foo.df,
          panel=function(...){histogram(...);
                              histogram(~ x, data=bar.df, col=NULL)})

(我的猜测是我需要使用panel.superpose,但是此功能有些令人困惑.Sarkar的书没有解释如何使用它,R帮助页面没有示例.我发现很难在没有基本了解的情况下理解panel.superpose帮助页面的意思.虽然我在网络上发现了很少的示例,但是我无法弄清楚这些示例的哪些方面适用于我的案例.这个答案确实是相关的,但是我不理解它对panel.groups的使用,示例覆盖了三个不同的地方分组,而我想将相同的数据重复覆盖在同样具有不同数据的多个面板上.)

(My guess is that I need to use panel.superpose, but this function is somewhat confusing. Sarkar's book doesn't explain how to use it, and the R help page has no examples. I'm finding it difficult to make sense of the panel.superpose help page without already having a basic understanding. There are a very small number of examples that I've found on the web, but I have been unable to figure out what aspects of those examples apply to my case. This answer is surely relevant, but I don't understand its use of panel.groups, and the example overlays three different groups from a single dataframe, whereas I want to repeatedly overlay the same data on multiple panels that also have different data .)

推荐答案

我继续研究此问题,并给出了答案.我一直走在正确的道路上,但弄错了一些关键细节.以下代码中的注释阐明了重点.

I continued working on this problem, and came up with an answer. I had been on the right track but got several crucial details wrong. Comments in the code below spell out important points.

# Main data, which will be displayed as solid histograms, different in each panel:
foo.df <- data.frame(y=rnorm(40), cat=c(rep("A", 20), rep("B", 20)))
# Comparison data: This will be displayed as an outline histogram in each panel:
bar.df <- data.frame(y=rnorm(30)-2)

# Define some vectors that we'll use in the histogram call.
# These have to be adjusted for the data by trial and error.
# Usually, panel.histogram will figure out reasonable default values for these.
# However, the two calls to panel.histogram below may figure out different values,
# producing pairs of histograms that aren't comparable.
bks <- seq(-5,3,0.5)  # breaks that define the bar bins
yl <- c(0,50)         # height of plot

# The key is to coordinate breaks in the two panel.histogram calls below.
# The first one inherits the breaks from the top-level call through '...' .
# Using "..." in the second call generates an error, so I specify parameters explicitly.
# It's not necessary to specify type="percent" at the top level, since that's the default,
# but it is necessary to specify it in the second panel.histogram call.
histogram(~ y | cat, data=foo.df, ylim=yl, breaks=bks, type="percent", border="cyan",
          panel=function(...){panel.histogram(...)
                              panel.histogram(x=bar.df$y, col="transparent",
                                              type="percent", breaks=bks)})

# col="transparent" is what makes the second set of bars into outlines.
# In the first set of bars, I set the border color to be the same as the value of col
# (cyan by default) rather than using border="transparent" because otherwise a filled
# bar with the same number of points as an outline bar will be slightly smaller.

这篇关于如何在每个面板上叠加直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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