ggplot2在函数或S4中使用时会产生错误

时间:2011-08-21 20:15:09

标签: r ggplot2

在功能或S4中使用时,我很难使用ggplot2。这是我没有功能的代码:

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)

,输出是 enter image description here

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

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)

修改

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

错误是

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

任何帮助和/或评论都将受到高度赞赏。提前谢谢。

4 个答案:

答案 0 :(得分:3)

与原始版本相比,函数中的版本在开头时缺少以下两行:

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

您是否尝试过使用debug()逐步查看函数来查找错误来源?

答案 1 :(得分:2)

只需在数据框中添加label列即可。 此外,请注意,您的内部aes永远不会有。{/ p>

这应该有效:

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)

答案 2 :(得分:2)

如果您希望能够使用rownames(G),则必须在ggplot2调用之外设置as.data.frame(G)。

在对geom_text的调用中设置data = as.data.frame(G)。

答案 3 :(得分:0)

p <- p + geom_text(aes(label = row.names(G)), size = 3, vjust = 1.25, colour = "black")

您为它提供了一个图层,但没有要绘制的数据。将label=移至您对ggplot的原始调用中,或者为geom_text()提供data=参数