I have a function provided to do a PCA plot which is shown below. I want to pass the names of the principal components(variables a and b) but have not figured a way to do this. I have tried using aes_string. Error received is Error in eval(expr, envir, enclos) : object '.names' not found
Per suggestion below, I have put a concrete example. One where the function works and another where it does not. Goal is to pass variables to this function.
#data
d = iris[1:4]
#########################################################################
# PCA_Plot functions
#########################################################################
PCA_Plot = function(pcaData)
{
library(ggplot2)
theta = seq(0,2*pi,length.out = 100)
circle = data.frame(x = cos(theta), y = sin(theta))
p = ggplot(circle,aes(x,y)) + geom_path()
loadings = data.frame(pcaData$rotation, .names = row.names(pcaData$rotation))
p + geom_text(data=loadings, mapping=aes(x = PC1, y = PC2, label = .names, colour = .names, fontface="bold")) +
coord_fixed(ratio=1) + labs(x = "PC1", y = "PC2")
}
# non-working function with two extra variables
PCA_Plot2 = function(pcaData, var1, var2)
{
library(ggplot2)
theta = seq(0,2*pi,length.out = 100)
circle = data.frame(x = cos(theta), y = sin(theta))
p = ggplot(circle,aes(x,y)) + geom_path()
loadings = data.frame(pcaData$rotation, .names = row.names(pcaData$rotation))
p + geom_text(data=loadings, mapping=aes(x = var1, y = var2, label = .names, colour = .names, fontface="bold")) +
coord_fixed(ratio=1) + labs(x = var1, y = var2)
}
#pca
library(stats)
p = prcomp(d)
PCA_Plot(p) #works
PCA_Plot2(p, "PC1", "PC2") # ERROR Error: Discrete value supplied to
continuous scale