当在函数或S4中使用时,ggplot2会产生错误 [英] ggplot2 produces error when used in function or S4

查看:83
本文介绍了当在函数或S4中使用时,ggplot2会产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在函数或S4中使用ggplot2时,我遇到了困难时期。这里是我的代码没有函数:

$ pre $ $ $ c $ rm(list = ls(all = TRUE))
library(nlme)
数据(燕麦)
数据< - 燕麦

数据$ Env< - 因子(数据$块)
数据$ Gen< - 因子$ Variety)

数据$ Gen < - 因子(数据$ Gen)
数据$ Env < - 因子(数据$ Env)
Gen.No < - 长度(水平(数据$ Gen))
Env.No < - 长度(水平(数据$ Env))
Min.GE < - min(Gen.No,Env.No)

GGE.ANOVA< - AOV(收率〜包膜+ ENV:根,数据=数据)
GGE.Effs< - model.tables(GGE.ANOVA,类型= 效果,cterms =Env:Gen)$ tables $Env:Gen
SVD <-svd(GGE.Effs)
D < - diag(SVD $ d [1:Min.GE]) (1)至(b)中的每一个,其中,每一个元素都包含一个或多个元素。 GE)
Ecolnames< - 糊( PC,Ecolnumb,九月= )
dimnames(E)< - 列表(水平(数据$ ENV),Ecolnames)
dimnames (G)< - 列表(等级(数据$ Gen),Ecolnames)
SVD.Values <-SVD $ d
PC.No <-c(1:长度(SVD.Values) )
PC.SS&l t; - SVD.Values ^ 2
PC.Percent.SS< - PC.SS / sum(PC.SS)* 100

library(grDevices)
con。 hull.pos< - chull(G)
con.hull< - rbind(G [con.hull.pos,],G [con.hull.pos [1],])

getPerpPoints< - 函数(mat){
x < - mat [,1]
y < - mat [,2]
out < - matrix(0,nrow = 2,ncol = 2)
if(diff(x)== 0){
xnew < - x [1]
}
else {
xnew < ; - (diff(y)/ diff(x))* x [1] -y [1]
xnew <-xnew /(diff(y)/ diff(x)+ diff(x)/ diff (y))
}
y new < - - (diff(x)/ diff(y))* xnew
out [2,] < - c(xnew,ynew)$ (1)(nrow(con.hull)-1),
函数(i)getPerpPoints(con(b))返回(out = out)
}
tmp< .hull [i:(i + 1),])[2,]))
tmp< - as.data.frame(tmp)


library(ggplot2 )
r < - 0.08
p < - ggplot(data = as.data.frame(G),aes(PC1,PC2))+ geom_point()+ theme_bw()
p< - p + geom_text(aes(label = row.names(G)),size = 3,vjust = 1.25,colou (数据= as.data.frame(con.hull),aes(PC1,PC2))
p <-p + geom_segment(data = as.data.frame(E),aes(xend = PC1,yend = PC2),x = 0,y = 0,
color =black,arrow =箭头(angle = 25,length = unit (data = as.data.frame(E),aes(label = row.names(E)),
size = 3, (x = sprintf(PC1(%.1f %%)),PC.Percent.SS [1]),$ b(颜色=黑色)
p < - p + labs $ by = sprintf(PC2(%.1f %%),PC.Percent.SS [2])))
p < - p + opts(axis.title.x = theme_text(size = 10, ),$ b $ p

p <-p + xlim(范围(c(E [,1],G [,1]))+范围(c(E [,1],G [,1]))* r)
p < - p + ylim (范围(c(E [,2],G [,2]))+范围(c(E [,2],G [,2]))* r)
p <-p + geom_segment data = as.data.frame(tmp),aes(xend = tmp $ V1,yend = tmp $ V2),x = 0,y = 0)
print(p)

,输出是

但是当我在下面的函数中使用相同的代码时,我得到错误

  rm(list = ls(all = TRUE))
PlotGGE < - function(Response,Env ,Gen,Data){

Data $ Env < - 因子(数据$ Env)
数据$ Gen < - 因子(数据$ Gen)
Gen.No < ; - 长度(等级(数据$ Env))
Min.GE < - min(Gen.No,Env.No) - 长度(等级(数据$ Gen))
Env.No < - 长度

GGE.ANOVA< -aov(yield_Env + Env:Gen,data = Data)
GGE.Effs< - model.tables(GGE.ANOVA,type =effects ,cterms =Env:Gen)$ tables $Env:Gen
SVD <-svd(GGE.Effs)
D < - diag(SVD $ d [1: GE])
E <-SVD $ u%*%sqrt(D)
G <-SVD $ v%*%sqrt(D)
Ecolnumb <-c(1 :Min.GE)
Ecolnames< - paste(PC,Ecolnumb,sep =)
dimnames(E)< - list(levels(Data $ Env),Ecolnames)
dimnames(G)< - 列表(等级(数据$ Gen),Ecolnames)
SVD.Values <-SVD $ d
PC.No <-c(1:长度(SVD.Values))
PC.SS< - SVD.Values ^ 2
PC.Percent.SS< - PC.SS / sum(PC.SS)* 100

library(grDevices)
con.hull.pos < - chull(G)
con.hull <-rbind(G [con.hull.pos,],G [con.hull.pos [1],])

getPerpPoints< - function(mat){
x< - mat [,1]
y< - mat [,2]
out< - 矩阵(0 ,nrow = 2,ncol = 2)
if(diff(x)== 0){
xnew < - x [1]
}
else {
xnew < - (diff(y)/ diff(x))* x [1] -y [1]
xnew <-xnew /(diff(y)/ diff(x)+ diff(x )/ diff(y))
}
y new < - - (diff(x)/ diff(y))* xnew
out [2,] < (1)(nrow(con.hull)-1),
函数(i)(b) getPerpPoints(con.hull [i:(i + 1),])[2,]))
tmp< - as.data.frame(tmp)


库(ggplot2)
r < - 0.08
p < - ggplot(data = as.data.frame(G),aes(PC1, PC2))+ geom_point()+ theme_bw()
p <-p + geom_text(aes(label = row.names(G)),size = 3,vjust = 1.25,color =black)$ b (数据= as.data.frame(con.hull),aes(PC1,PC2))
p <-p + geom_segment(data = as.data.frame(E) ,aes(xend = PC1,yend = PC2),x = 0,y = 0,
color =black,arrow =箭头(angle = 25,length = unit(0.25,cm)))
p < - p + geom_text(data = as.data.frame(E),aes(label = row.names(E)),
size = 3,vjust = 1.35,color =black )
p < - p + labs(list(x = sprintf(PC1(%.1f %%),PC.Percent.SS [1]),
y = sprintf(PC2 %1f %%),PC.Percent.SS [2])))
p <-p + opts(axis.title.x = theme_text(size = 10,hjust = 0.54,vjust = 0) )
p < - p + xlim(range.title.y = theme_text(size = 10,angle = 90,vjust = 0.25))
p <-p + (c [E [,1],G [,1]))+范围(c(E [,1],G [,1]))* r)
p <-p + 2],G [,2]))+范围(c(E [,2],G [ ,2)])* r)
p <-p + geom_segment(data = as.data.frame(tmp),aes(xend = tmp $ V1,yend = tmp $ V2),x = 0,y = 0)
print(p)
}

library(nlme)
data(燕麦)
$ b

编辑

  Oats $ Env <  - 因子(Oats $ Block)
Oats $ Gen-因子(Oats $ Variety)
PlotGGE(Response = yield,Env = Env,Gen = Gen,Data = Oats)

错误是

 在row.names中出错(G):找不到对象'G'

任何帮助和/或评论将高度赞赏。

帧。
此外,请注意,您不应该在$ aes 中有一个$。



这应该工作: / b>

  G1 < -  data.frame(G,label = rownames(G))
E1 < - data。帧(E,label = rownames(E))

library(ggplot2)
r < - 0.08
p < - ggplot(data = G1,aes(PC1,PC2) )+ geom_point()+ theme_bw()
p <-p + geom_text(aes(label = label),size = 3,vjust = 1.25,color =black)
p <-p + geom_path(数据= as.data.frame(con.hull),AES(PC1,PC2))$ b $碱基< - p + geom_segment(数据= as.data.frame(E),AES(xend的= PC1,
x = 0,y = 0,color =black,
arrow =箭头(angle = 25,length = unit(0.25,cm)))
p < - p + geom_text(data = E1,aes(label = label),
size = 3,vjust = 1.35,color =black)
p < - p + labs = sprintf(PC1(%.1f %%),PC.Percent.SS [1]),
y = sprintf(PC2(%.1f %%),PC.Percent.SS [2] )))
p < - p + opts(axis.title.x = theme _text(size = 10,hjust = 0.54,vjust = 0))
p <-p + opts(axis.title.y = theme_text(size = 10,angle = 90,vjust = 0.25))
p <-p + xlim(范围(c(E [,1],G [,1]))+范围(c(E [,1],G [,1]))* r)
p < -p + ylim(范围(c(E [,2],G [,2]))+范围(c(E [,2],G [,2]))* r)
p< ; - p + geom_segment(data = tmp,aes(xend = V1,yend = V2),x = 0,y = 0)
print(p)
pre>

I've tough time with ggplot2 when used in function or S4. Here is my code without function:

rm(list=ls(all=TRUE))
library(nlme)
data(Oats)
Data <- Oats

Data$Env <- factor(Data$Block)
Data$Gen  <- factor(Data$Variety)

Data$Gen <- factor(Data$Gen)
Data$Env <- factor(Data$Env)
Gen.No <- length(levels(Data$Gen))
Env.No <- length(levels(Data$Env))
Min.G.E <- min(Gen.No, Env.No)

GGE.ANOVA <- aov(yield ~ Env + Env:Gen, data = Data)
GGE.Effs <- model.tables(GGE.ANOVA, type = "effects", cterms = "Env:Gen")$tables$"Env:Gen"
SVD <- svd(GGE.Effs)
D <- diag(SVD$d[1:Min.G.E])
E <- SVD$u%*%sqrt(D)
G <- SVD$v%*%sqrt(D)
Ecolnumb <- c(1:Min.G.E)
Ecolnames <- paste("PC", Ecolnumb, sep="")
dimnames(E) <- list(levels(Data$Env), Ecolnames)
dimnames(G) <- list(levels(Data$Gen), Ecolnames)
SVD.Values <- SVD$d
PC.No <- c(1:length(SVD.Values))
PC.SS <- SVD.Values^2
PC.Percent.SS <- PC.SS/sum(PC.SS)*100

library(grDevices)
con.hull.pos <- chull(G)
con.hull <- rbind(G[con.hull.pos, ], G[con.hull.pos[1], ])

getPerpPoints <- function(mat) {
x <- mat[,1]
y <- mat[,2]
out <- matrix(0, nrow = 2, ncol = 2)
if(diff(x) == 0) {
xnew <- x[1]
  }
  else {
xnew <- (diff(y) / diff(x)) * x[1] - y[1]
xnew <- xnew / (diff(y) / diff(x) + diff(x) / diff(y))
  }
  ynew <- -(diff(x) / diff(y)) * xnew
  out[2,] <- c(xnew, ynew)
  return(out = out)
    }
tmp <- t(sapply(1:(nrow(con.hull)-1),
     function(i) getPerpPoints(con.hull[i:(i+1),])[2, ]))
tmp <- as.data.frame(tmp)


library(ggplot2)
r <- 0.08
p <- ggplot(data = as.data.frame(G), aes(PC1, PC2)) + geom_point() + theme_bw()
p <- p + geom_text(aes(label = row.names(G)), size = 3, vjust = 1.25, colour = "black")
p <- p + geom_path(data = as.data.frame(con.hull), aes(PC1, PC2))
p <- p + geom_segment(data = as.data.frame(E), aes(xend = PC1, yend = PC2), x = 0, y = 0,
                  colour = "black", arrow = arrow(angle = 25, length = unit(0.25, "cm")))
p <- p + geom_text(data = as.data.frame(E), aes(label = row.names(E)),
               size = 3, vjust = 1.35, colour = "black")
p <- p + labs(list(x = sprintf("PC1 (%.1f%%)", PC.Percent.SS[1]),
               y = sprintf("PC2 (%.1f%%)", PC.Percent.SS[2])))
p <- p  + opts(axis.title.x = theme_text(size = 10, hjust = 0.54, vjust = 0))
p <- p  + opts(axis.title.y = theme_text(size = 10, angle = 90,  vjust = 0.25))
p <- p + xlim(range(c(E[ ,1], G[ ,1])) + range(c(E[ ,1], G[ ,1]))*r)
p <- p + ylim(range(c(E[ ,2], G[ ,2])) + range(c(E[ ,2], G[ ,2]))*r)
p <- p + geom_segment(data = as.data.frame(tmp), aes(xend = tmp$V1, yend = tmp$V2), x = 0, y = 0)
print(p)

and the output is

But when I use the same code in the following function, I got error

rm(list=ls(all=TRUE))
PlotGGE <- function(Response, Env, Gen, Data) {

Data$Env <- factor(Data$Env)
Data$Gen  <- factor(Data$Gen)
Gen.No <- length(levels(Data$Gen))
Env.No <- length(levels(Data$Env))
Min.G.E <- min(Gen.No, Env.No)

GGE.ANOVA <- aov(yield ~ Env + Env:Gen, data = Data)
GGE.Effs <- model.tables(GGE.ANOVA, type = "effects", cterms = "Env:Gen")$tables$"Env:Gen"
SVD <- svd(GGE.Effs)
D <- diag(SVD$d[1:Min.G.E])
E <- SVD$u%*%sqrt(D)
G <- SVD$v%*%sqrt(D)
Ecolnumb <- c(1:Min.G.E)
Ecolnames <- paste("PC", Ecolnumb, sep="")
dimnames(E) <- list(levels(Data$Env), Ecolnames)
dimnames(G) <- list(levels(Data$Gen), Ecolnames)
SVD.Values <- SVD$d
PC.No <- c(1:length(SVD.Values))
PC.SS <- SVD.Values^2
PC.Percent.SS <- PC.SS/sum(PC.SS)*100

library(grDevices)
con.hull.pos <- chull(G)
con.hull <- rbind(G[con.hull.pos, ], G[con.hull.pos[1], ])

getPerpPoints <- function(mat) {
x <- mat[,1]
y <- mat[,2]
out <- matrix(0, nrow = 2, ncol = 2)
if(diff(x) == 0) {
xnew <- x[1]
  }
  else {
xnew <- (diff(y) / diff(x)) * x[1] - y[1]
xnew <- xnew / (diff(y) / diff(x) + diff(x) / diff(y))
  }
  ynew <- -(diff(x) / diff(y)) * xnew
  out[2,] <- c(xnew, ynew)
  return(out = out)
    }
tmp <- t(sapply(1:(nrow(con.hull)-1),
     function(i) getPerpPoints(con.hull[i:(i+1),])[2, ]))
tmp <- as.data.frame(tmp)


library(ggplot2)
r <- 0.08
p <- ggplot(data = as.data.frame(G), aes(PC1, PC2)) + geom_point() + theme_bw()
p <- p + geom_text(aes(label = row.names(G)), size = 3, vjust = 1.25, colour = "black")
p <- p + geom_path(data = as.data.frame(con.hull), aes(PC1, PC2))
p <- p + geom_segment(data = as.data.frame(E), aes(xend = PC1, yend = PC2), x = 0, y = 0,
                  colour = "black", arrow = arrow(angle = 25, length = unit(0.25, "cm")))
p <- p + geom_text(data = as.data.frame(E), aes(label = row.names(E)),
               size = 3, vjust = 1.35, colour = "black")
p <- p + labs(list(x = sprintf("PC1 (%.1f%%)", PC.Percent.SS[1]),
               y = sprintf("PC2 (%.1f%%)", PC.Percent.SS[2])))
p <- p  + opts(axis.title.x = theme_text(size = 10, hjust = 0.54, vjust = 0))
p <- p  + opts(axis.title.y = theme_text(size = 10, angle = 90,  vjust = 0.25))
p <- p + xlim(range(c(E[ ,1], G[ ,1])) + range(c(E[ ,1], G[ ,1]))*r)
p <- p + ylim(range(c(E[ ,2], G[ ,2])) + range(c(E[ ,2], G[ ,2]))*r)
p <- p + geom_segment(data = as.data.frame(tmp), aes(xend = tmp$V1, yend = tmp$V2), x = 0, y = 0)
print(p)
}

library(nlme)
data(Oats)

EDIT

Oats$Env <- factor(Oats$Block)
Oats$Gen <- factor(Oats$Variety)
PlotGGE(Response = yield, Env = Env, Gen = Gen, Data = Oats)

The error is

Error in row.names(G) : object 'G' not found

Any help and/or comments will be highly appreciated. Thanks in advance.

解决方案

Just add a label columns to the data frames. Also, note that you should never have a $ inside aes.

This should work:

G1 <- data.frame(G, label=rownames(G))
E1 <- data.frame(E, label=rownames(E))

library(ggplot2)
r <- 0.08
p <- ggplot(data=G1, aes(PC1, PC2)) + geom_point() + theme_bw()
p <- p + geom_text(aes(label=label), size=3, vjust=1.25, colour="black")
p <- p + geom_path(data=as.data.frame(con.hull), aes(PC1, PC2))
p <- p + geom_segment(data=as.data.frame(E), aes(xend = PC1, yend=PC2),
                      x=0, y=0, colour="black",
                      arrow=arrow(angle=25, length=unit(0.25, "cm")))
p <- p + geom_text(data=E1, aes(label=label),
                   size=3, vjust=1.35, colour="black")
p <- p + labs(list(x=sprintf("PC1 (%.1f%%)", PC.Percent.SS[1]),
                   y=sprintf("PC2 (%.1f%%)", PC.Percent.SS[2])))
p <- p  + opts(axis.title.x=theme_text(size=10, hjust=0.54, vjust=0))
p <- p  + opts(axis.title.y=theme_text(size=10, angle=90, vjust=0.25))
p <- p + xlim(range(c(E[ ,1], G[ ,1])) + range(c(E[ ,1], G[ ,1]))*r)
p <- p + ylim(range(c(E[ ,2], G[ ,2])) + range(c(E[ ,2], G[ ,2]))*r)
p <- p + geom_segment(data=tmp, aes(xend=V1, yend=V2), x=0, y=0)
print(p)

这篇关于当在函数或S4中使用时,ggplot2会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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