【问题标题】:How to incorporate visualization of log2-transformation in function?如何将 log2 转换的可视化合并到函数中?
【发布时间】:2021-05-29 13:39:30
【问题描述】:

在这个thread 的帮助下,我拥有了这个norm_f() 函数。该函数生成两个图来评估正态分布:

> norm_f(iris$Sepal.Length)

函数是这样写的:

norm_f <- function(z, hist. = list(col = "lightgray"),
                   lines. = list(lwd = 2), curve. = list(col = "red", lwd = 2),
                   qqnorm. = list(col = "black", pch = 1, cex = 1.2), qqline. = list(lwd = 2)) {
  zname <- deparse(substitute(z))
  zden <- density(z)
  if (!"ylim" %in% names(hist.)) {
    ymax <- max(zden$y, dnorm(c(seq(min(z), max(z), len = 21)), mean = mean(z), sd = sd(z)))
    hist.$ylim <- c(0, ymax)
  }
  par(mfrow=c(1,2), mar=c(5,5,2,2))
  if (!"main" %in% names(hist.)) hist.$main <- paste("Histogram of", zname)
  if (!"xlab" %in% names(hist.)) hist.$xlab <- zname
  do.call(hist, c(list(z,  prob = TRUE), hist.))
  do.call(lines, c(list(zden), lines.))
  do.call(curve, c(list(substitute(dnorm(x, mean=mean(z), sd=sd(z))),
                        add = TRUE), curve.))
  do.call(qqnorm, c(list(z), qqnorm.))
  do.call(qqline, c(list(z), qqline.))
  
}

我想更新函数,使其生成 2x2 图:两个没有 log2 变换(如上)和两个带有 log2 变换协变量的图(即,对应于 norm_f(log2(iris$Sepal.Length))

预期输出:

【问题讨论】:

    标签: r function dataframe plot


    【解决方案1】:

    我修改了原始函数norm_f() 以使用ggplot2patchwork。现在该函数接受三个参数:df 是你的 data.frame,var 是一个代表你的目标变量的字符(例如,Sepal.Length)和thefun,它调用你想要的不带引号的缩放函数。这些图是通过 ggplot2 渲染的,它们的布局是通过 patchwork 组织的。

    请查看下面的代码以检查它是否符合您的需求。我运行了两个示例,一个使用请求的log2() 函数,第二个使用exp()

    library(ggplot2)
    library(patchwork)
    
    data(iris)
    
    norm_f <- function( df, var, thefun, ... ) {
      
      # this gets the name of the function invoked
      fun_name = as.character(match.call()[4]) 
      # this matches the function in `thefun` so that you can use it
      thefun = match.fun(thefun)
      
      # compute the log2
      va2_log2 <- thefun(df[ , var ])
      
      # specify we want the density for the histogram
      y <- "..density.."
      # Histogram of "var"
      p_hist <- ggplot( df, aes_string(x = var) ) +
        geom_histogram(aes_string( y = y ) ) + 
        geom_density() +
        stat_function(fun = dnorm, 
                      args = list(mean = mean(df[ , var ], na.rm = TRUE), 
                                  sd = sd(df[ , var ], na.rm = TRUE)), 
                      colour = 'blue') + 
        ggtitle(paste0("Histogram of ", var) )
      
      # QQ-plot of "var"
      p_qq <- ggplot( df, aes_string(sample = var) ) +
        stat_qq() + stat_qq_line() +
        ggtitle(paste0("QQ-Plot of ", var) )
      
      # Histogram of log2("var")
      p_hist_log <- ggplot( df, aes_string(x = va2_log2) ) +
        geom_histogram(aes_string( y = y ) ) + 
        geom_density() +
        stat_function(fun = dnorm, 
                      args = list(mean = mean(va2_log2, na.rm = TRUE), 
                                  sd = sd(va2_log2, na.rm = TRUE)), 
                      colour = 'blue') +
        ggtitle( paste0("Histogram of ", fun_name, "(", var, ")" ) )
      
      # QQ-plot of log2("var")
      p_qq_log <- ggplot( df, aes_string(sample = va2_log2) ) +
        stat_qq() + stat_qq_line() +
        ggtitle( paste0("QQ-Plot of ", fun_name, "(", var, ")" ) )
      
      print((p_hist + p_qq) / (p_hist_log + p_qq_log))
      
    }
    
    norm_f(df = iris, var = "Sepal.Length", thefun = log2 )
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    

    norm_f(df = iris, var = "Sepal.Length", thefun = exp )
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    

    reprex package (v1.0.0) 于 2021-03-01 创建

    【讨论】:

    • 嗨@FrancescoGrosetti。非常感谢,很好的解决方案。我喜欢指定参数的想法。衍生问题:是否可以添加一个log 参数,它采用值210ln(默认2),以便norm_f 产生未转换的histogram + QQ plot 和对数转换的 histogramQQ plot 取决于 log=2,10 or ln?
    • 请查看更新后的回复。这应该可以完成工作。
    猜你喜欢
    • 2021-05-11
    • 1970-01-01
    • 2018-08-21
    • 2020-02-16
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多