I am attempting to plot my dataset using PCA in R in Windows 10. But while plotting I got the message
zero-length 'labels' specified
Below are the data set and code:
Taxon,L/D1 ,L/D4,L/DL,Di.Pro.,ST,H.Fo/H.Ch
1,3,2.9,2.28,200,2,1
1,1.33,2.3,2.44,225,2,1
1,2,2.52,2.4,150,2,1
1,3.5,2.75,2.32,125,2,1
1,3.18,2.81,2,125,2,1
1,2.35,3,2.82,125,2,1
1,2.72,2.28,2.09,250,2,1
2,3.6,2.82,2.65,125,4,1
2,2.5,2.7,3.02,150,4,1
2,3.2,2.6,2.7,150,4,1
2,2.4,3.3,2.7,200,4,1
2,3,2.87,3.08,175,4,1
3,2,2.83,2,100,1,0.67
3,4,2.66,2.26,100,1,0.67
3,2.33,2.78,2.28,150,1,0.67
3,2,2.83,2.3,100,1,0.67
4,2.83,2.83,2.66,200,1,0.66
4,3.66,2.57,2.27,100,1,0.66
4,3.33,2.9,2.25,150,1,0.66
4,4.33,3.09,2.15,125,1,0.66
4,1.85,2.44,2.43,225,1,0.66
4,2.85,2.57,2.65,175,1,0.66
4,2.8,2.55,2.2,200,1,0.66
4,1.85,2.57,2.04,175,1,0.66
5,2.83,2.5,3,125,3,0.66
5,2.8,4,3.14,200,3,0.66
5,2,3.5,2.4,200,3,0.66
5,3,3.42,2.9,150,3,0.66
5,2.4,2.6,2.71,175,3,0.66
5,4,3.37,2.52,125,3,0.66
5,1.75,2.69,2.9,250,3,0.66
5,2,2.54,2.76,200,3,0.66
5,2.4,3.25,2.46,175,3,0.66
6,3.5,3.14,2.48,75,5,1
6,2.25,2.16,2.1,75,5,1
6,3,3.23,2.77,125,5,1
6,2.5,2.3,1.85,100,5,1
6,2.95,2.71,2.14,100,5,1
6,2.5,3.4,3.09,125,5,1
6,2,2.57,2.3,100,5,1
6,3,2.55,2.46,125,5,1
7,3,4.76,2.94,100,1,1
7,1.66,2.77,2.2,200,1,1
7,2.16,2.8,2.4,250,1,1
8,2.25,3.09,2.83,150,3,1
8,3.5,3.5,2.88,125,3,1
8,3.33,2.87,3.04,150,3,1
library(vegan)
daz= read.csv (file.choose())
cor(daz)
daz.pca<-rda(daz)
summary(daz.pca)
PCA1,2
daz.taxon.pca <- rda(daz[,c(2:7)],scale=T)
par(pty="s")
plot(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),type="n",xlab="PCA1 (99.69%)",ylab="PCA2 (0.19%)",xlim=c (-8.00,8.00),ylim=c(-2.00,1.50))
abline(v=0,lty=3)
abline(h=0,lty=3)
arrows(0,0,scores(daz.taxon.pca,display="species",choices=1),scores(daz.taxon.pca,display="species",choices=2),length=0.10,col="dark gray")
text(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),as.character(daz$taxon),cex=0.7)
Error in text.default(scores(daz.taxon.pca, display = "sites", choices
= 1), : zero-length 'labels' specified
Any help? I am rather new to R, and failed to find any explanation of this error. The code in general seems fine, except when I add
text(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),as.character(daz$taxon),cex=0.7)
for the plot, the error comes up. Any hint would be greatly appreciated.
You use daz$taxon instead of daz$Taxon to access the data in daz variable which return back NULL.
Use the following code at the end of your analysis and it should work fine:
text(scores(daz.taxon.pca, display="sites", choices=1),
scores(daz.taxon.pca, display="sites", choices=2),
as.character(daz$Taxon),
cex=0.7)
Here's my code:
iFacVector <- as.factor(c(1,1,1,1,10,1,1,1,12,9,9,1,10,12,1,9,5))
iTargetVector <- c(2,1,0,1,6,9,15,1,8,0,1,2,1,1,9,12,1)
bp <- plot(iFacVector,iTargetVector)
text(bp,tapply(iTargetVector,iFacVector,median),labels=tapply(iTargetVector,iFacVector,median),cex=.8)
I am getting the following (classic R) error:
Error in xy.coords(x, y, recycle = TRUE) :
(list) object cannot be coerced to type 'double'
The vectors I am passing are numeric so I don't know what the problem is. I have tried unlist() and as.vector(). I have also tried using bp$stats[3,] as the labels.
The help for text gives the arguments as
text(x, ...)
so the first argument in your code, bp, is being treated as the x coordinate for where to place the text. You can just leave off the bp and get better behavior. You might also want to add pos=3 to get a nicer placement of the text.
I think I have a problem with the scope of input$variable. Following is the code description.
In my ui.R file I have a selectInput that defines the value taken by input$variable in my server.R file. The selectInput is as follows:
selectInput("variable", "Variable:",
c("Handling of Complaints" = "complaints",
"Does not allow special privileges" = "privileges",
"Opportunity to Learn" = "learning",
"Raises based on Performance" = "raises",
"Too Critical" = "critical",
"Advancement" = "advance")),
In my server.R file I have a renderUI that uses this input$variable inside a with and inside a qplot as follows:
grid <- with(attitudeData, expand.grid(
xgrid = seq(min(input$variable), max(input$variable), length = dim(attitudeData)[1]),
ygrid = levels(sex)
))
ggattitude <- qplot(x=input$variable, y=rating,
data=attitudeData, color=sex) + geom_line(data=grid)
attitudeData is the dataframe with variables (aka features or predictors) with names as defined in the selectInput.
If I use any of the variable names, say complaints, directly in the code it works and my plot is generated the way I want for that variable. But with input$variable I can't get it to work. This is why I think the problem has to do with the scope of input$variable.
Any help would be appreciated.
Following is the error I get from shiny which isn't very helpful to me.
Error in seq.default(min(input$variable), max(input$variable), length = dim(attitudeData)[1]) : 'from' cannot be NA, NaN or infinite
If I replace input$variable with complaints in the with, the error for the use in qplot is as follows:
Error in eval(expr, envir, enclos) : object 'complaints' not found
Thanks in advance for any help I get.
You can add print statements to clarify what R sees as input. In your server file:
grid <- with(attitudeData, expand.grid(
print(input$variable)
xgrid = seq(min(input$variable), max(input$variable), length = dim(attitudeData)[1]),
ygrid = levels(sex)
))
ggattitude <- qplot(x=input$variable, y=rating,
data=attitudeData, color=sex) + geom_line(data=grid)
This output will be printed to the R console. I have a feeling that your input isn't rendering as "complaints".
What should min("complaints") return? So you are giving string as argument to function min() it is different from min(complaints) if that is what you want.
Or is there some other problem like:
xgrid = seq(min(input$variable), max(input$variable), length = dim(attitudeData)[1])
I am wondering if you really try to do:
xgrid = seq(min(attitudeData[input$variable]), max(attitudeData[input$variable]), length = dim(attitudeData)[1])