开发者

Understanding xyplot in R

开发者 https://www.devze.com 2023-04-08 18:53 出处:网络
I\'m an R newbie and I\'m trying to understand the xyplot function in lattice. I have a dataframe: df <- data.frame(Mean=as.vector(abc), Cycle=seq_len(nrow(abc)), Sample=rep(colnames(abc), each=

I'm an R newbie and I'm trying to understand the xyplot function in lattice.

I have a dataframe:

df <- data.frame(Mean=as.vector(abc), Cycle=seq_len(nrow(abc)), Sample=rep(colnames(abc), each=nrow(abc)))

and I can plot it using

xyplot(Mean ~ Cycle, group=Sample, df, type="b", pch=20, auto.key=list(lines=TRUE, points=FALSE, columns=2), file="abc-quality")

My question is, what are Mean and Cycle? Looking at ?xyplot I can see that this is some kind of function and I understand they are coming from the data frame df, but I can't see them with ls() and >Mean gives Error: object 'Mean' not found. I tried to replicate the plot by substituting df[1] and df[2] for Mean and Cycle respectively thinking that these would be equal but that doesn't seem to be the case. Could someone explain what data types these are (objects, variables, etc) and if there is a generic way to access them (lik开发者_StackOverflow社区e df[1] and df[2])?

Thanks!

EDIT: xyplot works fine, I'm just trying to understand what Mean and Cycle are in terms of how they relate to df (column labels?) and if there is a way to put them in the xyplot function without referencing them by name, like df[1] instead of Mean.


These are simply references to columns of df.

If you'd like access them by name without mentioning df every time, you could write with(df,{ ...your code goes here... }). The ...your code goes here... block can access the columns as simply Mean and Cycle.

A more direct way to get to those columns is df$Mean and df$Cycle. You can also reference them by position as df[,1] and df[,2], but I struggle to see why you would want to do that.

The reason your xyplot call works is it that implicitly does the equivalent of with(df), where df is your third argument to xyplot. Many R functions are like this, for example lm(y~x,obs) would also correctly pick up columns x and y from dataframe obs.


You need to add , data=df to your call to xyplot():

xyplot(Mean ~ Cycle, data=df,                  # added data= argument
       group=Sample, type="b", pch=20, 
       auto.key=list(lines=TRUE, points=FALSE, columns=2), 
       file="abc-quality")

Alternatively, you can with(df, ....) and place your existing call where I left the four dots.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号