r:如何使函数返回由 par() 排列的两个绘图对象 [英] r: How to make a function return two plot objects arranged by par()

查看:40
本文介绍了r:如何使函数返回由 par() 排列的两个绘图对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是 functions 的新手,我正在尝试创建一个简单的 function,它返回由 par() 排列的两个图,例如

So I am new to functions and I am trying to create a simple function that returns two plots arranged by par(), like

par(mfrow=c(1,2), mar=c(5,5,2,2))
hist(p$vitd,  col='lightgray', prob=T)
lines(density(p$vitd), lwd=2)
curve(dnorm(x, mean=mean(p$vitd), sd=sd(p$vitd)), 
      col="red", lwd=2, add=TRUE)
qqnorm(p$vitd) 
qqline(p$vitd)

给出预期的输出:

所以我只能弄清楚如何返回一个对象

So I can only figure out how to return one object

喜欢

norm_f <- function(y){
  
list(qqnorm(y),qqline(y))
  
}

norm_f(p$vitd)

#或

norm_f <- function(y){
  
list(hist(y,  col='lightgray', prob=T),
     lines(density(y), lwd=2),
     curve(dnorm(x, mean=mean(y), sd=sd(y)), 
           col="red", lwd=2, add=TRUE))
  
}

norm_f(p$vitd)

我试过 return(list(), list())- 但没有用.

I tried return(list(), list())- but that did not work.

问题:如何让 function 返回两个绘图对象?

Question: how can I make the function return the two plot objects?

此外,在运行 norm_f(p$vitd) 时,function 会自动在 Console 中打印大量文本 - 我该怎么办摆脱它?

Also, when running norm_f(p$vitd), the function automatically prints a lot of text in the Console - how can I get rid of that?

数据

p <- structure(list(vitd = c(28.5, 32.9, 60.3, 50.5, 15, 47.8, 78.7, 
16.3, 71.9, 32.1, 33.2, 20.3, 33.5, 25.3, 89.8, 39.8, 27.6, 15.1, 
61.8, 50.8, 72.1, 13.1, 18.3, 67.1, 60.2, 53.7, 54.6, 11.4, 48.5, 
78.1, 29.1, 55.4, 75.1, 69.3, 91.2, 19.7, 26.9, 54.5, 25, 84.4, 
15.6, 76.6, 45, 39.4, 67.6, 53.9, 93.6, 43.9, 25.8, 49.6, 46.1, 
46.5, 57.3, 61.4, 29.8, 67.8, 64.4, 59.4, 44, 58.8, 5.2, 23.8, 
26.7, 42.3, 19.9, 72.4, 54.2, 69.5, 70.1, 22.1, 46.3, 52.2, 61.7, 
50.7, 46.9, 28, 29.4, 29.7, 72.4, 96.6, 55.5, 36.2, 36.2, 61.8, 
76.7, 43.5, 41.9, 76.8, 90, 44.4, 33, 46, 28.2, 47.7, 40.7, 41.4, 
53.2, 53.9, 35.3, 14.9)), row.names = c(NA, 100L), class = "data.frame")

推荐答案

这是您想要做的吗?

norm_f <- function(z, hist. = list(col = "lightgray"),
                   lines. = list(lwd = 2), curve. = list(col = "red", lwd = 2),
                   qqnorm. = list(col = "gray70", pch = 16, cex = 0.5), 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.))
}

norm_f(iris$Sepal.Length)

这篇关于r:如何使函数返回由 par() 排列的两个绘图对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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