ggplot2 produces error when used in function or S4

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 ![enter image description here][1] But when I use the same code in the following function, I got error [1]: http://i.stack.imgur.com/jsW2V.jpg 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.
Well, your code is not the same in both instances. The first line in your PlotGGE function is Data$Env <- factor(Data$Env). Since you pass Oats to your function, this is equivalent to Oats$Env <- factor(Oats$Env). Since Oats doesn't have an Env column, this causes your error. This has nothing to do with ggplot.

以上就是ggplot2 produces error when used in function or S4的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏