2013-01-15 39 views
0

这是一个跟随从昨天开始的muy问题。下面我想添加一个链接将文本表达式添加到xy图中,自动定位

prior question

最新的代码是:

mypanel <- function(x,y,...) { 
    panel.xyplot(x, y, ...) 
    panel.grid(x=-1, y=-1) 
    panel.lmline(x,y,col="red",lwd=1,lty=1) 
    panel.text(200,20,bquote(rho == .(correls[x])),cex=.8, font = 2,col="black") 
} 

correls <- as.vector(cor(x=mtcars[,2:3],y=mtcars[,1])) 
correls<- round(coeff,3) 
names(correls)<-names(mtcars[,2:3]) 

data <- mtcars[,2:3] 
charts <- lapply(names(data), function(x) { xyplot (mtcars[,1] ~ mtcars[,x], 
                panel=mypanel,ylab="MPG", xlab=x)}) 

的代码需要datset mtcars以及格子,我认为这可能需要LtticeExtra一报还一。

正如你所看到的,我计算了相关系数,并希望将它们添加到图表中。文本武功添加,但有2个问题:

  1. 的部分correls [X],因为我想不评价,我得到一个NA(它的工作原理本身在控制台不过,我总是检查发现的错误)。我似乎不知道如何解决这个问题,即使是一个指针也能帮上忙,我很乐意去做腿部工作
  2. 在某些图表中坐标不起作用,因为这是在lapply中运行的,这意味着有些不可见Inthis不是第一个图表,是第二个图表)。是否有可能使该位置自动?阿欣只是一个指针,如何寻找和使用认识

再次感谢

马里奥

更新,解决方案与面板序号建议做鼻涕工作,由lapply所以单独创建的每个图表它总是1,所以我没有得到我需要的东西。我修改了代码以尝试处理位置和开始坐标。然而,它并不总是showand这也说明从CORREL错误的元素出于某种原因,一看就知道oneneeds手动打印使用图表1图等

这里是最新的代码

mypanel <- function(x,y,...) { 
    panel.xyplot(x, y, ...) 
    panel.grid(x=-1, y=-1) 
    panel.lmline(x,y,col="red",lwd=1,lty=1) 
    panel.text(xmax[x],ymax,bquote(rho == .(correls[x])),pos=4,cex=1, font = 2,col="black") 
} 

correls <- as.vector(cor(x=mtcars[,2:10],y=mtcars[,1])) 
correls<- round(correls,3) 
names(correls)<-names(mtcars[,2:10]) 
xmax <-sapply(mtcars[,2:10],max) 
names(xmax) <- names(mtcars[,2:10]) 
xmax<-floor(xmax) 
ymax <- floor(max(mtcars[,1])) 


data <- mtcars[,2:10] 
charts <- lapply(names(data), function(x) { xyplot (mtcars[,1] ~ mtcars[,x], 
                panel=mypanel,ylab="MPG", xlab=x, 
                xlim=c(0,ceiling(max(mtcars[,x]))) 
                ,ylim=c(0,ceiling(max(mtcars[,1]))))}) 

再次感谢任何指针

马里奥

+0

是否由于没有注意到在这个循环调用中有“coeff”而不是“correls”?除此之外,Lattice中还有一些函数可以让您知道您正在使用哪个面板。这将是构建索引以访问正确'correls'值的方式。 –

+0

非常感谢你的支持 - 虽然这会产生一个不正确的值(当然我不会这样做),但这不是问题所在。相关值中包含值,该值不会在标签中找到。一个标签被放置在正确的rho和等号显示中,但correl [x]评估为NA,所以我看到rho = NA(在希腊字母当然有排) 另一个问题是如果我完全可以拥有在可见区域的标签位置,因为坐标不同,它们不能显示。可以这样做吗?我有一个R功能可以自动放置标签? – user1617979

+0

没问题。这是我的观点,即要改变你为'correls'向量编制索引的方式。 –

回答

0

我终于解决了这个问题,我使用的功能的multiplot我发现GGPLOT2,主要是因为它给我(它的语法),也因为我喜欢它的图表的外观更有意义。

这里是整个代码,包括我在“Cookbook for R”上找到的多点功能(非常感谢)。

这可以进一步改善,因为现在我需要重新命名用作我的数据的数据集。我意识到我可以将所有这些都包含在一个函数中,但是现在这对我来说并不是太麻烦。

我希望这可以帮助别人。

最后,我会说我对文本放置不是很满意,理想情况下它会寻找空的空间,但我想这不会那么容易。如果有人知道如何随意分享。

#You need to create an object called my data with your data.frame 
#the process will create charts of correlations for the first column versus all others 
#and then arrange them in a lattice patter. 
#It uses the multiplot function that I found as well as ggplot2 

mydata<-mtcars 

library(ggplot2) 
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { 
    require(grid) 

    # Make a list from the ... arguments and plotlist 
    plots <- c(list(...), plotlist) 

    numPlots = length(plots) 

    # If layout is NULL, then use 'cols' to determine layout 
    if (is.null(layout)) { 
    # Make the panel 
    # ncol: Number of columns of plots 
    # nrow: Number of rows needed, calculated from # of cols 
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), 
        ncol = cols, nrow = ceiling(numPlots/cols)) 
    } 

    if (numPlots==1) { 
    print(plots[[1]]) 

    } else { 
    # Set up the page 
    grid.newpage() 
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) 

    # Make each plot, in the correct location 
    for (i in 1:numPlots) { 
     # Get the i,j matrix positions of the regions that contain this subplot 
     matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) 

     print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, 
             layout.pos.col = matchidx$col)) 
    } 
    } 
} 

mychart <- function(x) { 
    c <- round(cor(mydata[,1],mydata[x]),3) 
    xmax <-ceiling(max(mydata[,x])) 
    xmin <- floor(min(mydata[,x])) 
    xpos = floor(max(mydata[,x])*(8/10)) 
    ypos = floor(max(mydata[,1])*(8/10)) 
    t = paste("rho ==",c,sep="") 
    t1 <- annotate("text",x=xpos,y=ypos,label=t,parse=TRUE,color="red") 
    p <- qplot(mydata[,x],mydata[,1],xlab=x,ylab=names(mydata)[1],color=I("blue")) 
    s <- stat_smooth(aes(x=mydata[,x],y=mydata[,1]),method="lm",color="red",se=FALSE) 
    a <- annotate("text",x=xpos,y=ypos,label=t,parse=TRUE,color="red") 
    p<- p+s+a+xlim(xmin,xmax) 

} 


charts <- NULL 
l1 <- NULL 
for (i in 2:(length(mydata))) { 
    charts[[i]]<- mychart(names(mydata)[i]) 
} 

numcols <- ceiling(sqrt(length(mydata)-1)) 

multiplot(plotlist=charts[2:length(mydata)],cols=numcols) 
相关问题