使用 lapply() 或 walk() 生成直方图时更改直方图中的标签

Changing the labels in histogram when using lapply() or walk() to produce histogram

我正在尝试使用来自 purrr 包的 lapply() 或 walk() 创建几个直方图的矩阵。

这是我的数据集的捏造版本,仅包含 11 列中的 5 列和大约 100 行中的 3 列:

pid 性别 兰特 BP GH VT
1 F D 5 7 5
2 一个 6 10 5
3 F D 0 30 5

这是我正在使用的代码,我想添加一些东西来根据 i 值更改 x 标签。

    x <- datf #dataframe
    u <- x[,4:11]
    par(mfrow=c(2,4)) 
        walk(x[,4:11], 
                 function(i) 
                      {hist(i[x$rand=="D"], 
                            col=rgb(0,0,1,0.2), 
                            main = "Histogram of score",
                            ylim=c(0,100))
                        hist(i[x$rand=="A"], 
                            col=rgb(1,0,0,0.2), 
                            add=TRUE)})

我使用了 lapply() 而不是 walk() - 但是为了隐藏 Rmarkdown 文档中的输出更改为 walk()。

在阅读了类似的问题后,我尝试使用xlab = paste(colnames(i))xlab = paste(colnames(u)) and Labels for histogram, when using “lapply”

xlab = paste(colnames(u)) 是最接近的,但直方图中的 x 标签不是正确的,而是所有这些的列表。 请看图片。 Image



但是,当我创建一个类似的直方图但直方图中只有一组数据时,即不包括 hist(i[x$rand=="A"], col=rgb(1,0,0,0.2), add=TRUE)。它工作正常。

mapply(hist, as.data.frame(x[,4:11]), main=colnames(x[,4:11]), xlab="score")

我创建了一个示例数据集,它的形式看起来像我的,请参阅代码。

 Library("dplyr")
    datf <- data.frame(cbind(sample(0:100,size=150, replace=T), 
                             sample(0:100,size=150, replace=T), 
                             sample(0:100,size=150, replace=T),
                             sample(0:100,size=150, replace=T),
                             sample(0:100,size=150, replace=T),
                             sample(0:100,size=150, replace=T),
                             sample(0:100,size=150,replace=T),
                             sample(0:100,size=150, replace=T)))
    datf$rand <- sample(c("D","A"),150, replace=T, prob=c(0.45,0.45))
    datf$pid <- sample(1:150, replace=F, size=150)
    datf$gender <- sample(c("F","M"),150, replace=T, prob=c(0.35,0.65))
    datf <- datf%>%
      rename(
        BP=X1,
        GH=X2,
        VT=X3,
        MH=X4, 
        SF=X5, 
        PF=X6,
        RP=X7,
        RE=X8
      )
    datf <- datf[, c("pid","rand","gender", "BP", "GH","VT","MH", "PF" , "RP", "RE","SF")]

和dput()

structure(list(pid = c(108L, 54L, 75L, 2L), rand = c("A", "A", 
"A", "A"), gender = c("M", "M", "F", "M"), BP = c(70L, 13L, 27L, 
66L), GH = c(2L, 68L, 61L, 19L), VT = c(57L, 68L, 30L, 0L), MH = c(65L, 
69L, 21L, 47L), PF = c(100L, 38L, 70L, 60L), RP = c(77L, 27L, 
59L, 38L), RE = c(66L, 9L, 68L, 48L), SF = c(30L, 74L, 64L, 20L
)), row.names = c(NA, 4L), class = "data.frame")

这是我希望输出的样子: See image here

使用 ggplot 会更容易吗? - 但那又如何呢?

提前致谢!

也许这样的东西更接近您要找的东西?

library(tidyverse)

datf %>%
  pivot_longer(cols = BP:SF) %>%
  ggplot() + aes(value, fill = rand) + 
  geom_histogram() + facet_wrap(~name)