Note that there are some explanatory texts on larger screens.

plurals
  1. POggplot2 produces error when used in function or S4
    primarykey
    data
    text
    <p>I've tough time with ggplot2 when used in function or S4. Here is my code without function:</p> <pre><code>rm(list=ls(all=TRUE)) library(nlme) data(Oats) Data &lt;- Oats Data$Env &lt;- factor(Data$Block) Data$Gen &lt;- factor(Data$Variety) Data$Gen &lt;- factor(Data$Gen) Data$Env &lt;- factor(Data$Env) Gen.No &lt;- length(levels(Data$Gen)) Env.No &lt;- length(levels(Data$Env)) Min.G.E &lt;- min(Gen.No, Env.No) GGE.ANOVA &lt;- aov(yield ~ Env + Env:Gen, data = Data) GGE.Effs &lt;- model.tables(GGE.ANOVA, type = "effects", cterms = "Env:Gen")$tables$"Env:Gen" SVD &lt;- svd(GGE.Effs) D &lt;- diag(SVD$d[1:Min.G.E]) E &lt;- SVD$u%*%sqrt(D) G &lt;- SVD$v%*%sqrt(D) Ecolnumb &lt;- c(1:Min.G.E) Ecolnames &lt;- paste("PC", Ecolnumb, sep="") dimnames(E) &lt;- list(levels(Data$Env), Ecolnames) dimnames(G) &lt;- list(levels(Data$Gen), Ecolnames) SVD.Values &lt;- SVD$d PC.No &lt;- c(1:length(SVD.Values)) PC.SS &lt;- SVD.Values^2 PC.Percent.SS &lt;- PC.SS/sum(PC.SS)*100 library(grDevices) con.hull.pos &lt;- chull(G) con.hull &lt;- rbind(G[con.hull.pos, ], G[con.hull.pos[1], ]) getPerpPoints &lt;- function(mat) { x &lt;- mat[,1] y &lt;- mat[,2] out &lt;- matrix(0, nrow = 2, ncol = 2) if(diff(x) == 0) { xnew &lt;- x[1] } else { xnew &lt;- (diff(y) / diff(x)) * x[1] - y[1] xnew &lt;- xnew / (diff(y) / diff(x) + diff(x) / diff(y)) } ynew &lt;- -(diff(x) / diff(y)) * xnew out[2,] &lt;- c(xnew, ynew) return(out = out) } tmp &lt;- t(sapply(1:(nrow(con.hull)-1), function(i) getPerpPoints(con.hull[i:(i+1),])[2, ])) tmp &lt;- as.data.frame(tmp) library(ggplot2) r &lt;- 0.08 p &lt;- ggplot(data = as.data.frame(G), aes(PC1, PC2)) + geom_point() + theme_bw() p &lt;- p + geom_text(aes(label = row.names(G)), size = 3, vjust = 1.25, colour = "black") p &lt;- p + geom_path(data = as.data.frame(con.hull), aes(PC1, PC2)) p &lt;- 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 &lt;- p + geom_text(data = as.data.frame(E), aes(label = row.names(E)), size = 3, vjust = 1.35, colour = "black") p &lt;- p + labs(list(x = sprintf("PC1 (%.1f%%)", PC.Percent.SS[1]), y = sprintf("PC2 (%.1f%%)", PC.Percent.SS[2]))) p &lt;- p + opts(axis.title.x = theme_text(size = 10, hjust = 0.54, vjust = 0)) p &lt;- p + opts(axis.title.y = theme_text(size = 10, angle = 90, vjust = 0.25)) p &lt;- p + xlim(range(c(E[ ,1], G[ ,1])) + range(c(E[ ,1], G[ ,1]))*r) p &lt;- p + ylim(range(c(E[ ,2], G[ ,2])) + range(c(E[ ,2], G[ ,2]))*r) p &lt;- p + geom_segment(data = as.data.frame(tmp), aes(xend = tmp$V1, yend = tmp$V2), x = 0, y = 0) print(p) </code></pre> <p>and the output is <img src="https://i.stack.imgur.com/jsW2V.jpg" alt="enter image description here"></p> <p>But when I use the same code in the following function, I got error</p> <pre><code>rm(list=ls(all=TRUE)) PlotGGE &lt;- function(Response, Env, Gen, Data) { Data$Env &lt;- factor(Data$Env) Data$Gen &lt;- factor(Data$Gen) Gen.No &lt;- length(levels(Data$Gen)) Env.No &lt;- length(levels(Data$Env)) Min.G.E &lt;- min(Gen.No, Env.No) GGE.ANOVA &lt;- aov(yield ~ Env + Env:Gen, data = Data) GGE.Effs &lt;- model.tables(GGE.ANOVA, type = "effects", cterms = "Env:Gen")$tables$"Env:Gen" SVD &lt;- svd(GGE.Effs) D &lt;- diag(SVD$d[1:Min.G.E]) E &lt;- SVD$u%*%sqrt(D) G &lt;- SVD$v%*%sqrt(D) Ecolnumb &lt;- c(1:Min.G.E) Ecolnames &lt;- paste("PC", Ecolnumb, sep="") dimnames(E) &lt;- list(levels(Data$Env), Ecolnames) dimnames(G) &lt;- list(levels(Data$Gen), Ecolnames) SVD.Values &lt;- SVD$d PC.No &lt;- c(1:length(SVD.Values)) PC.SS &lt;- SVD.Values^2 PC.Percent.SS &lt;- PC.SS/sum(PC.SS)*100 library(grDevices) con.hull.pos &lt;- chull(G) con.hull &lt;- rbind(G[con.hull.pos, ], G[con.hull.pos[1], ]) getPerpPoints &lt;- function(mat) { x &lt;- mat[,1] y &lt;- mat[,2] out &lt;- matrix(0, nrow = 2, ncol = 2) if(diff(x) == 0) { xnew &lt;- x[1] } else { xnew &lt;- (diff(y) / diff(x)) * x[1] - y[1] xnew &lt;- xnew / (diff(y) / diff(x) + diff(x) / diff(y)) } ynew &lt;- -(diff(x) / diff(y)) * xnew out[2,] &lt;- c(xnew, ynew) return(out = out) } tmp &lt;- t(sapply(1:(nrow(con.hull)-1), function(i) getPerpPoints(con.hull[i:(i+1),])[2, ])) tmp &lt;- as.data.frame(tmp) library(ggplot2) r &lt;- 0.08 p &lt;- ggplot(data = as.data.frame(G), aes(PC1, PC2)) + geom_point() + theme_bw() p &lt;- p + geom_text(aes(label = row.names(G)), size = 3, vjust = 1.25, colour = "black") p &lt;- p + geom_path(data = as.data.frame(con.hull), aes(PC1, PC2)) p &lt;- 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 &lt;- p + geom_text(data = as.data.frame(E), aes(label = row.names(E)), size = 3, vjust = 1.35, colour = "black") p &lt;- p + labs(list(x = sprintf("PC1 (%.1f%%)", PC.Percent.SS[1]), y = sprintf("PC2 (%.1f%%)", PC.Percent.SS[2]))) p &lt;- p + opts(axis.title.x = theme_text(size = 10, hjust = 0.54, vjust = 0)) p &lt;- p + opts(axis.title.y = theme_text(size = 10, angle = 90, vjust = 0.25)) p &lt;- p + xlim(range(c(E[ ,1], G[ ,1])) + range(c(E[ ,1], G[ ,1]))*r) p &lt;- p + ylim(range(c(E[ ,2], G[ ,2])) + range(c(E[ ,2], G[ ,2]))*r) p &lt;- 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) </code></pre> <p><strong>EDIT</strong></p> <pre><code>Oats$Env &lt;- factor(Oats$Block) Oats$Gen &lt;- factor(Oats$Variety) PlotGGE(Response = yield, Env = Env, Gen = Gen, Data = Oats) </code></pre> <p>The error is </p> <pre><code>Error in row.names(G) : object 'G' not found </code></pre> <p>Any help and/or comments will be highly appreciated. Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload