如何创建镜像直方图 [英] How to create mirrored histograms

查看:75
本文介绍了如何创建镜像直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示 2 个组的倾向得分匹配统计数据(在 BW 中不匹配,在颜色上匹配),并想使用如下所示的镜像直方图

I would like present propensity score matching stats on 2 groups (unmatched in BW, matched in color) and would like to use mirrored histograms like the following

是否可以在基础 R 中叠加 4 个不同的直方图?是否有提供此功能的软件包?

Is it possible to overlay 4 different histograms in base R? Is there any package providing this functionality?

x 轴是 [0,1] 有界(这是一个概率)并且 BW 列总是大于或等于彩色列(也就是说,在彩色列后面"不能有 BW 列).

The x-axis is [0,1] bounded (it is a probability) and the BW columns are always bigger or equal to the colored columns (that is there cannot be BW columns "behind" the colored columns).

图片来自 http://www.ncbi.nlm.nih.gov/pubmed/22244556

推荐答案

您可以使用以下内容.您可能需要预先计算 hist 对象以获得正确的 ylim 值,然后使用 axismtexttitle 以正确标记您的图表.

You can use something like the following. You would want to pre-calculate the hist objects to get the correct ylim values, then use axis and mtext or title to properly label your graph.

set.seed(1234)
x <- rnorm(100, 0, 1)

plot.new()
plot.window(ylim = c(-40, 40), xlim = range(x))
p <- list(axes = FALSE, xlab = "", ylab = "", main = "")
par(new = TRUE)  
do.call(hist, c(list(x = x, ylim = c(-40, 40)), p))
par(new = TRUE)
do.call(hist, c(list(x = x, ylim = c(40, -40)), p))
axis(side = 2, 
     at = pretty(par()$usr[3:4]), 
     labels = abs(pretty(par()$usr[3:4])))
axis(side = 1)

编辑

## Create some fake data
set.seed(1234)
d <- rnorm(250, 0, 1)
e <- rnorm(250, 1, 1)
f <- rlnorm(100, 0, .2)
g <- rlnorm(100, 1, .2)

## Function for plotting
multhist <- function(..., bin.width, col, dir, xlab = NULL, ylab = NULL,
                     main = NULL) {

  vals <- list(...)
  vrng <- range(vals)

  brks <- seq(vrng[1] - abs(vrng[1]*0.1), 
              vrng[2] + abs(vrng[2]*0.1), 
              by = bin.width)

  yrng <- max(sapply(lapply(vals, hist, breaks = brks), "[[", "counts"))
  yrng <- 1.2*c(-1*yrng, yrng)

  plot.new()
  plot.window(ylim = yrng, xlim = vrng)

  addhist <- function(x, col, dir) {
    par(new = TRUE)  
    hist(x = x, ylim = dir*yrng, col = col, xlab = "", ylab = "", 
         main = "", axes = FALSE, breaks = brks)
  }

  mapply(addhist, x = vals, col = col, dir = dir)

  py <- pretty(yrng)
  py <- py[py >= yrng[1] & py <= yrng[2]]
  axis(side = 2, at = py, labels = abs(py))
  axis(side = 1)
  title(main = main, xlab = xlab, ylab = ylab)

}

您可以为函数提供数值向量,以及对应颜色和方向(1 或 -1)的向量.我没有对 vals、col 和 dir 的长度进行正式检查,但它非常简单.

You can give the function numeric vectors, as well as vectors for the corresponding colors and directions (1 or -1). I did not do the formal checking on the lengths of vals, col, and dir, but it is pretty straight forward.

## Use the function
multhist(d, e, f, g, bin.width = 0.5, 
         col = c("white", "white", "lightgreen", "darkgreen"), 
         dir = c(1, -1, 1, -1), xlab = "xlabel", ylab = "ylabel", 
         main = "title")

这篇关于如何创建镜像直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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