gpt4 book ai didi

r - 如何创建镜像直方图

转载 作者:行者123 更新时间:2023-12-04 10:24:38 25 4
gpt4 key购买 nike

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



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

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

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

最佳答案

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

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)

enter image description here

编辑
## 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 的长度进行正式检查,但它非常简单。
## 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")

enter image description here

关于r - 如何创建镜像直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29928712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com