使用panel.superpose在直方图中显示具有不同边框的组 [英] Display groups with different borders in histogram with panel.superpose

查看:142
本文介绍了使用panel.superpose在直方图中显示具有不同边框的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案显示了如何使用groupspanel.superpose在同一面板中显示重叠的直方图,并为每个直方图分配不同的颜色.另外,我想给每个直方图一个不同的边框颜色. (这将使我可以将一个直方图显示为无边框的实心条,并覆盖有透明的全边界直方图.为了清楚起见,下面的示例有所不同.)

This answer shows how to use groups and panel.superpose to display overlapping histograms in the same panel, assigning different colors to each histogram. In addition, I want to give each histogram a different border color. (This will allow me to display one histogram as solid bars without a border, overlayed with a transparent, all-border histogram. The example below is a little different for the sake of clarity.)

尽管可以使用border=在图中使用不同的边框颜色,但由于col=的填充颜色,它们并未分配给组.如果给border=提供一系列颜色,则似乎一次在它们之间循环显示一个小节.如果两个直方图重叠,则效果会有点愚蠢(见下文).

Although it's possible to use border= to use different border colors in the plot, they are not assigned to groups as fill colors are with col=. If you give border= a sequence of colors, it seems to cycle through them one bar at at time. If the two histograms overlap, the effect is a bit silly (see below).

是否可以为每个组指定特定的边框颜色?

Is there a way to give each group a specific border color?

# This illustrates the problem: Assignment of border colors to bars ignores grouping:

# make some data
foo.df <- data.frame(x=c(rnorm(10),rnorm(10)+2), cat=c(rep("A", 10),rep("B", 10)))

# plot it
histogram(~ x, groups=cat, data=foo.df, ylim=c(0,75), breaks=seq(-3, 5, 0.5), lwd=2,
          panel=function(...)panel.superpose(..., panel.groups=panel.histogram, 
                                             col=c("transparent", "cyan"), 
                                             border=c(rep("black", 3), rep("red", 3))))

请注意,您不能只计算每组中有多少根小节,并在border设置中将这些数字提供给rep.如果两个直方图重叠,则至少一个直方图将使用两种边框颜色.

Note that you can't just count how many bars there are in each group and provide those numbers to rep in the border setting. If the two histograms overlap, at least one of the histograms will use two border colors.

(是panel.superpose代码,将组放置在同一面板上并分配颜色.我对此没有深刻的了解.)

(It's the panel.superpose code that places the groups on the same panel and that assigns the colors. I don't have a deep understanding of it.)

推荐答案

panel.histogram()没有正式的groups=参数,如果您检查其代码,就会发现它可以处理任何提供的groups=参数与panel.*()函数不同,并且以不太标准的方式.设计决定的结果是(如您所发现的)通常不容易将指定每组外观的图形参数矢量传递给它

panel.histogram() doesn't have a formal groups= argument, and if you examine its code, you'll see that it handles any supplied groups= argument differently and in a less standard way than panel.*() functions that do. The upshot of that design decision is that (as you've found) it's not in general easy to pass in to it vectors of graphical parameters specifying per-group appearance

作为一种解决方法,我建议使用 latticeExtra +()as.layer()函数来覆盖多个单独的histogram()图,每组一个.这是您可能的操作方式:

As a workaround, I'd suggest using latticeExtra's +() and as.layer() functions to overlay a number of separate histogram() plots, one for each group. Here's how you might do that:

library(lattice)
library(latticeExtra)

## Split your data by group into separate data.frames
foo.df <- data.frame(x=c(rnorm(10),rnorm(10)+2), cat=c(rep("A", 10),rep("B", 10)))
foo.A <- subset(foo.df, cat=="A")
foo.B <- subset(foo.df, cat=="B")

## Use calls to `+ as.layer()` to layer each group's histogram onto previous ones  
histogram(~ x, data=foo.A, ylim=c(0,75), breaks=seq(-3, 5, 0.5), 
          lwd=2, col="transparent", border="black") +
as.layer(
histogram(~ x, data=foo.B, ylim=c(0,75), breaks=seq(-3, 5, 0.5), 
          lwd=2, col="cyan", border="red") 
)

这篇关于使用panel.superpose在直方图中显示具有不同边框的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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