R 向xy打印添加文本表达式,自动定位

R 向xy打印添加文本表达式,自动定位,r,R,siAll,这是对昨天的muy问题的跟进。下面我尝试添加一个链接 最新代码为: 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") }

siAll,这是对昨天的muy问题的跟进。下面我尝试添加一个链接

最新代码为:

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)})

mypanel我终于解决了这个问题,我使用了我找到的multiplot函数和ggplot2,主要是因为它对我更有意义(它的语法),而且我喜欢它的图表外观

下面是完整的代码,包括我在“R的食谱”中找到的multiplot函数(非常感谢)

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

我希望这对某人有帮助

最后,我要说的是,我对文本的位置不太满意,理想情况下,它会寻找空白,但我想这并不容易。如果有人知道如何自由分享

#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)
#您需要使用data.frame创建一个名为my data的对象
#该过程将创建第一列与所有其他列的相关性图表
#然后把它们排列成格子状。
#它使用我找到的multiplot函数以及ggplot2

MyDatas是其中的一部分,因为您没有注意到在
轮调用中使用了“coeff”而不是“correls”?除此之外,Lattice中还有一些函数可以告诉您正在使用哪个面板。这将是建立索引以访问正确的
correls
值的方法。感谢您捕捉到了这一点-当然,这将创建一个不正确的值(我会更正它),这不是问题所在。correls中有值,但该值不会在标签中拾取。标签放置在正确的位置,显示为rho和等号,但correl[x]的计算结果为NA,因此我看到rho=NA(当然是希腊字母的行)。另一个问题是,如果我能将标签放置在可见区域中,因为坐标的差异足够大,某些坐标无法显示。那能做到吗?我有一个R函数,可以自动放置标签?完全正确。这就是我关于更改为
correls
向量编制索引的方式的观点。部分问题无疑是由于“x”的多次使用,但如果我更改传递的列名的名称,我无法找出如何让bquote或substitute接受
correls[x]
。我删除了我的答案。无法让它工作。
图表
对象是一个列表,应该可以向其中添加元素。最近有一些问题证明了这一过程。尝试搜索。
#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)