Error ploting sequences in TraMineR R - r

Just starting to experiment TraMiner, after having read the (very good) User Guide
I managed to create the sequences from my data, but as I'm trying to plot I got the following errors:
> seqiplot(my.data$sequences, title="My first sequences", withlegend = TRUE)
Error in do.call(plot, args = plist) :
'what' must be a string or a function
Where does this come from and what can I do about it?

I think you get an error because you override the plot function. This reproduce the error:
plot <- 1
do.call(plot,list(0))
Error in do.call(plot, list(0)) :
'what' must be a character string or a function
This should ork:
rm(plot)
seqiplot(my.data$sequences, title="My first sequences", withlegend = TRUE)

I guess the error shows up because you restricted your sequence state object to the single variable my.data$sequences. Have you tried my.data instead?

Related

expression function used in ggpubr::ggarrange

This is my code. The object fig was a list with three ggplot outcomes.
mylabel <- expression("PM"[2.5], "SO"[2], "O"[3])
ggpubr::ggarrange(fig[[1]], fig[[2]], fig[[3]], ncol=1, nrow=3, labels = mylabel)
However, I got error like this:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ""expression"" to a data.frame
Does anyone know the reason? Is this my programming mistake? OR expression function just could not be used in ggpubr::ggarrange function? Thanks a lot.

Error message when ploting with "R"

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)

Adding data labels to boxplot in R

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.

R shiny - correct use of an input variable, seems like a scope error

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])

r - Add text to each lattice histogram with panel.text but has error "object x is missing"

In the following R code, I try to create 30 histograms for the variable allowed.clean by the factor zip_cpt(which has 30 levels).
For each of these histograms, I also want to add mean and sample size--they need to be calculated for each level of the factor zip_cpt. So I used panel.text to do this.
After I run this code, I had error message inside each histogram which reads "Error using packet 21..."x" is missing, with..." (I am not able to read the whole error message because they don't show up in whole). I guess there's something wrong with the object x. Is it because mean(x) and length(x) don't actually apply to the data at each level of the factor zip_cpt?
I appreciate any help!
histogram(~allowed.clean|zip_cpt,data=cpt.IC_CAB1,
type='density',
nint=100,
breaks=NULL,
layout=c(10,3),
scales= list(y=list(relation="free"),
x=list(relation="free")),
panel=function(x,...) {
mean.values <-mean(x)
sample.n <- length(x)
panel.text(lab=paste("Sample size = ",sample.n))
panel.text(lab=paste("Mean = ",mean.values))
panel.histogram(x,col="pink", ...)
panel.mathdensity(dmath=dnorm, col="black",args=list(mean=mean(x, na.rm = TRUE),sd=sd(x, na.rm = TRUE)), ...)})
A discussion I found online is helpful for adding customized text (e.g., basic statistics) on each of the histograms:
https://stat.ethz.ch/pipermail/r-help/2007-March/126842.html

Resources