使用 sapply() 在 R 中绘制标题 [英] Plot titles in R using sapply()

查看:35
本文介绍了使用 sapply() 在 R 中绘制标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 sapply() 从我的数据框中的每个变量中制作 qqnorm 图.这是我到目前为止所得到的:

I want to make qqnorm plot out of every variable in my data frame using sapply(). This is what I've got so far:

myfun=function(x) {
  c(qqnorm(x),
    qqline(x)
  )
}

sapply(mydata, myfun)

它有效,但是,我希望每个图在图的标题中都有各自的变量名称.这是怎么做的?

It works, however, I'd like each plot to have the respective variable name in the title of the plot. How is this done?

非常感谢;-)

推荐答案

在这种情况下 l_ply 更合适,因为您只需要绘图,因此不需要输出.基于@Henrik 的回答,我们有

In this case l_ply is more suitable because you just need to plot and therefore no output is needed. Based on @Henrik answer we have

require(plyr)
myfun <- function(x, data, ...) {
  c(qqnorm(data[[x]], main = names(data)[x], ...),
    qqline(data[[x]])
  )
}

l_ply(seq_len(ncol(swiss)), myfun, data = swiss)

编辑

如果你想查看你的图表,你有很多选择,其中之一是将你的绘图设备分开并将每个 qqplot 绘制在设备的一个部分.

If you want to see your graphs, you have many options and one of them is to divide you plotting device and plot each qqplot in one part of the device.

par(mfrow = c(3, 2))
l_ply(seq_len(ncol(swiss)), myfun, data = swiss)

这篇关于使用 sapply() 在 R 中绘制标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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