R 如何在每个面板上叠加直方图
我想在每个格子直方图面板上叠加一个额外的直方图(在每个面板中都是相同的)。我希望叠加的直方图具有实心边框,但填充为空(R 如何在每个面板上叠加直方图,r,histogram,lattice,R,Histogram,Lattice,我想在每个格子直方图面板上叠加一个额外的直方图(在每个面板中都是相同的)。我希望叠加的直方图具有实心边框,但填充为空(col),以便与基础直方图进行比较 也就是说,最终结果将是一系列面板,每个面板具有不同的彩色直方图,并且每个面板在彩色直方图的顶部具有相同的额外轮廓直方图 以下是我尝试过的一些东西,但它只会产生空面板: foo.df <- data.frame(x=rnorm(40), categ=c(rep("A", 20), rep("B", 20))) bar.df <- da
col
),以便与基础直方图进行比较
也就是说,最终结果将是一系列面板,每个面板具有不同的彩色直方图,并且每个面板在彩色直方图的顶部具有相同的额外轮廓直方图
以下是我尝试过的一些东西,但它只会产生空面板:
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)})
foo.df我继续研究这个问题,并找到了答案。我一直走在正确的道路上,但几个关键的细节都搞错了。下面代码中的注释说明了要点
# 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.
#主数据将显示为实心直方图,在每个面板中不同:
df我举例说明了另一个部分解决方案,我提出了一个旨在帮助回答这个问题的问题。如果我在其他人回答之前学会了如何在这里提供这个问题的完整解决方案,我将把它作为一个答案发布。