ggbiplot used to work with no problems using prcomp but now does not. All I receive is the following error code:
Error in plot_label(p = p, data = plot.data, label = label, label.label = label.label, :
Unsupported class: prcomp
I have installed ggbiplot using dependencies=TRUE and everything else other posts about similar issues have done but yet I still get this message.
Any help is appreciated.
mypca <- prcomp(mydata, center=TRUE, scale.=TRUE)
ggbiplot(mypca, center=TRUE, scale.=TRUE)
Error in plot_label(p = p, data = plot.data, label = label, label.label = label.label, :
Unsupported class: prcomp
I don't think ggbiplot has a center nor scale. argument. Are you confusing prcomp with ggbiplot function arguments?
The following works just fine:
library(ggbiplot)
pca <- prcomp(USArrests, center = TRUE, scale. = TRUE)
ggbiplot(pca)
Tested on ggbiplot_0.55.
Try,
ggbiplot::ggbiplot(mypca)
Related
I am trying to plot the GMM of my dataset using the Mclust package in R. While the plotting is a success, I do not want points to show in the final plot, just the ellipses. For a reference, here is the plot I have obtained:
GMM Plot
But, I want the resulting plot to have only the ellipses, something like this:
GMM desired plot
I have been looking at the Mclust plot page in: https://rdrr.io/cran/mclust/man/plot.Mclust.html and looking at the arguments of the function, I see there is a scope of adding other graphical parameters. Looking at the documentation of the plot function, there is a parameter called type = 'n' which might help to do what I want but when I write it, it produces the following error:
Error in plot.default(data[, 1], data[, 2], type = "n", xlab = xlab, ylab = ylab, :
formal argument "type" matched by multiple actual arguments
For reference, this is the code I used for the first plot:
library(mclust)
Data1_2 <- Mclust(Data, G=15)
summary(Data1_2, parameters = TRUE, classification = TRUE)
plot(Data1_2, what="classification")
The code I tried using for getting the graph below is:
Data1_4 <- Mclust(Data, G=8)
summary(Data1_4, parameters = TRUE, classification = TRUE)
plot(Data1_4, what="classification", type = "n")
Any help on this matter will be appreciated. Thanks!
If you look under the source code of plot.Mclust, it calls plot.Mclust.classification which in turn calls coordProj for the dot and ellipse plot. Inside this function, the size is controlled by the option CEX= and shape PCH=.
So for your purpose, do:
library(mclust)
clu = Mclust(iris[,1:4], G = 3, what="classification")
plot(clu,what="classification",CEX=0)
I would like to plot the results of the robust PCA (pcaCoDa) from the robCompositions package using ggplot2.
Previously, it worked with ggbiplot (https://github.com/vqv/ggbiplot) however, I can no longer get it to work with my current R version (3.6.0).
Is there a way to do a biplot with the pcaCoda results with ggplot2 using CRAN packages?
Here is a working example without using ggplot:
library(robCompositions)
df <- arcticLake
a <- pcaCoDa(df)
biplot(a)
And another example without using the robust PCA, but using the autoplot function:
library(ggplot2)
autoplot(princomp(df))
However, I would like to use the robust PCA with ggplot/autoplot. When I try to plot it, i get the following error:
autoplot(a)
Error: Objects of type pcaCoDa not supported by autoplot.
I also tried the following and also get an error:
autoplot(a$princompOutputClr)
Error in scale.default(data, center = FALSE, scale = 1/scale) :
length of 'scale' must equal the number of columns of 'x'
Any advice? Thanks!
For some reasons that I ignore pcaCoda returns one value less for scale and center compared to the output of other pca methods such as prcomp or princomp. I think that's the reason why autoplot does not want to plot this object.
Alternatively, if you want to apply the robust algortithm, you can use the package pcaMethods available on bioconductor, here i provided an example using the iris dataset that you can found on the documentation of pcaMethods (https://bioconductor.org/packages/release/bioc/html/pcaMethods.html):
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("pcaMethods")
library(pcaMethods)
library(ggplot2)
robust = pca(iris[c(1, 2, 3, 4)], method = "robustPca", scale = "uv", center = TRUE)
iris = merge(iris, scores(robust), by =0)
ggplot(iris, aes( x= PC1, y = PC2, colour = Species))+
geom_point()+
stat_ellipse()
Does it look what you are trying to get ?
try to run the following code, but always got error message:
Error in text.default(x, y, txt, cex = cex, font = font) : invalid
mathematical annotation
par(mfrow=c(2,3))
x <- c(1:4,6:9)
myPlot<-function(x){
plot(pros.dat, pros.dat$svi ~ pros.dat[,x])
}
lapply(c(1:4,6:9), FUN=myPlot)
Could anybody tell me what's wrong? Thanks
R's plot function's definition is:
plot(x, y, ...)
So you should define your function as:
myPlot<-function(x){
plot(pros.dat$svi, pros.dat[,x])
}
I'm writing functions for an R-package which will use a wrapper function for ggpairs from the package GGally to plot the output objects of the methods. I would like ggpairs to be able to use variables not part of the input object for defining aesthetics but this produces an error message with ggpairs, see below for a minimal example:
library(GGally)
library(ggplot2)
# The data object
object <- list(x = iris[, 1:2], label = "Iris data")
# The grouping
y <- iris[, 5]
# The plotting function
wrapper <- function(object, mapping = aes()){
ggpairs(object$x, mapping)
}
# This works
wrapper(object)
# This doesn't work
wrapper(object, aes(color = y))
The latter one produces the error message:
Error in .subset(col, i) : object of type 'symbol' is not subsettable
Any trick to get the second plotting command to work without modifying the input object would be greatly appreciated.
I am trying to draw PCA results with ggbiplot, how can I draw supplementary variables ?
I found this discussion for MCA results, but I would like to have the arrows as well...
data(wine)
wine.pca <- PCA(wine, scale. = TRUE, quanti.sup = c(4,5))
plot(wine.pca)
ggbiplot(wine.pca)
Besides, this code gives me an error :
1: In sweep(pcobj$ind$coord, 2, 1/(d * nobs.factor), FUN = "*") :
STATS is longer than the extent of 'dim(x)[MARGIN]'
2: In sweep(v, 2, d^var.scale, FUN = "*") :
STATS is longer than the extent of 'dim(x)[MARGIN]'
I tried your code and didn't reproduce your error but had other problems. I googled PCA() and found the package used to do the PCA was FactoMineR. After looking at the documentation, I also changed scale. to scale.unit and quanti.sup to quali.sup, giving the correct columns the categorical variables are in.
library(FactoMineR)
data(wine)
wine.pca <- PCA(wine, scale.unit = TRUE, quali.sup = c(1,2))
plot(wine.pca)
ggbiplot(wine.pca)
That should give the correct output.