在应用中将数据框列名称用作图表标签 [英] Using dataframe column names as chart label in Apply

查看:77
本文介绍了在应用中将数据框列名称用作图表标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一系列x-y散点图,其中y始终是相同的变量,而x是我要检查它们是否相关的变量.例如,让我们使用mtcars数据集.

I want to create a series of x-y scatter charts, where y is always the same variable and x are the variables I want to check if they are correlated with. As an example lets use the mtcars dataset.

我对R还是比较陌生,但是越来越好.

I am relatively new to R but getting better.

下面的代码有效,列表图表包含所有图表,但X轴显示为"x",我希望它成为变量的名称.我尝试了xlab=的多种组合,但我似乎没有得到

The code below works, the list charts contains all the charts, except that the X axis shows as "x", and I want it to be the name of the variable. I tried numerous combinations of xlab= and I do not seem to get it

如果使用names(data),我会看到要使用的名称.我想我想引用names(data)的第一个,第一次引用apply,第二次,等等,该如何做?

if I use names(data) I see the names I want to use. I guess I want to reference the first of names(data) the first iteration of apply, the second the second time, etc. How can I do that?

下一步是将它们一起打印在一个格子中,我假设lapplysapply将通过print函数来解决问题-我也很欣赏这个主意,只是指针我不需要解决方案

Th next step would be to print them in a lattice together, I assume an lapply or sapply will do the trick with the print function - I appreciate idea for this too, just pointers I do not need a solution.

load(mtcars)
   mypanel <- function(x,y,...) {
   panel.xyplot(x,data[,y],...)
   panel.grid(x=-1,y=-1)
   panel.lmline(x,y,col="red",lwd=1,lty=1)
   } 
   data <- mtcars[,2:11]
   charts <- apply(data,2,function(x) xyplot (mtcars[,1] ~ x, panel=mypanel,ylab="MPG")) 

这一切都开始了,因为我无法使用面板功能来循环.

This all started because I was not able to use the panel function to cycle.

推荐答案

我没有发现此代码有效".对此进行了修改:

I did not find that this code "worked". Modified it to do so:

mypanel <- function(x,y,...) {
   panel.xyplot(x, y, ...)
   panel.grid(x=-1, y=-1)
   panel.lmline(x,y,col="red",lwd=1,lty=1)
   } 
data <- mtcars[,2:11]
charts <- lapply(names(data), function(x) { xyplot (mtcars[,1] ~ mtcars[,x], 
                                               panel=mypanel,ylab="MPG", xlab=x)})

需要从面板函数中删除"data [,y]"并传递名称而不是列向量,因此x标签需要使用某些东西.

Needed to remove the 'data[,y]' from the panel function and pass names instead of column vectors so there was something to use for a x-label.

这篇关于在应用中将数据框列名称用作图表标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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