Error message when ploting with "R" - 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)

Related

R error with gap.plot: origin must be supplied

I am using the gap.plot function to make a graph that spans across a multi-year data-set. However, with the first series I am trying to add it's already giving me issues with respect to the origin:
Error in as.Date.numeric(e) : 'origin' must be supplied
I looked at the typical things to try to resolve this, and I think I am using the correct function to transform the timestamps. The dataset looks fine.
My code is as follows:
setwd("~/PhD/4 Field experiment/Harvest data")
##### data weergeven #####
BioAvg<-read.csv("Alex Copy of Biomass (average).csv", header=T, na="", stringsAsFactors=TRUE)
class(BioAvg$Time)
class(BioAvg$Harvest)
class(BioAvg$Time)<-"Date"
BioAvg$Time2<- as.Date(BioAvg$Time, origin="1970-01-01")
BioAvg2<-BioAvg[1:15,]
plot.new()
par(bty="n")
gap.plot(BioAvg2$Time, BioAvg2$X0EW.0P, gap=c(17850,18000, 18230,18300), gap.axis = "x",pch = 19, col = "gray55",
type="o", lwd=2, ylab = "Aboveground biomass (g)", xlab= "Time",
xtics = c(17600:17850,18000:18230,18300:18430), xticlab = c(May:Nov,Jun:Nov,Apr:Jun))
P.s. there are more graphs to be added later on in the code, otherwise gap.plot would not make sense indeed. But this is the minimum code needed to reproduce the error.
The CSV file with data:
https://drive.google.com/file/d/1IdfiSnEeSHWu4s52l0RCgqTahNe-3cNL/view
Many thanks for the advice

R - apply survfit to a list and plot with corresponding names

I have a list of named dataframes:
library(survival)
library(survminer)
surv.days<- runif(n = 50, min = 0, max = 500)
censor<- sample(c(0,1), 50, replace=TRUE)
survdata<- data.frame(surv.days, censor)
survlist<- list(survdata, survdata)
names(survlist)<- c("name1", "name2")
rm(survdata, censor, surv.days)
I want to run a survfit on each dataframe and then generate several plots (I put only one here for the sake of simplicity), each plot with the corresponding title. I think Map is the way to do it, so:
titles<- names(survlist)
Then I define the function that I want to use to run the survival analysis and plots:
survival.function<- function(survivaldata, datanames){
sfit<- survfit(Surv(surv.days, censor)~1, data=survivaldata)
ggsurvplot(sfit, conf.int=TRUE, risk.table=TRUE,
surv.median.line = "v",
title=datanames,
risk.table.height=.25)
}
And try to apply it:
Map(survival.function, survlist, titles)
But the idea didn't work:
"Error in eval(fit$call$data) : object 'survivaldata' not found "
Is there a way to properly assign the objects to the survival functions?
Thank you!.
In this case the error message seems to be misleading, at least to my reading. It appears to point to the error being in the call to survfit when in fact it's an error within ggsurvplot as can be seen from the output of traceback(). I first tried to change the name of the object passed to survival.function. Then no error. But also no plot. So also added a print call inside survival.function. I'll attache the first of the two plots that result.
survival.function<- function(data, datanames){
sfit<- survfit(Surv(surv.days, censor)~1, data=data)
print( ggsurvplot(sfit, conf.int=TRUE, risk.table=TRUE,
surv.median.line = "v",
title=datanames,
risk.table.height=.25) )
}
I wish I could better explain why this hack works. (I'm only guessing at the cause on the basis of the error message and traceback()-results.) I'm using the fact that the name of the "missing" object was "data". It's possible this is a semantic bug in the ggsurvplot and you would be doing a favor to the maintainer of the package to send him a link to this fully documented example. I wonder whetehr there would be possible improvement if the maintainer changed the code so that the list member name were not accessed from the environment with $data but rather [[data]].

Error In Bar plot - incorrect number of names

I'm having trouble creating bar plots in R. The odd thing is that I've used the same script before with different variables without any issues, so I don't really know what is going on (I'm fairly new to R).
The code I've been using is (with transposed matrix):
barplot(StandtF, main="h2, c2, e2 by Mod FEMALES",
xlab="Social Support", ylab="% of Var", beside=TRUE, axesnames=TRUE,
names.arg=c("No","Yes"),
col=c("red","green","darkblue"))
legend("topleft",c("h2","c2","e2"),fill=c("red","green","darkblue") )
And the error message that's returned is:
Error in barplot.default(StandtM, main = "h2, c2, e2 by Mod FEMALES",
xlab = "Social Support", :
incorrect number of names
In addition: Warning message:
In plot.window(xlim, ylim, log = log, ...) :
"axesnames" is not a graphical parameter
I've looked around at different posts, but nothing so far has been helpful. And as said, the script has worked fine previously.
Any help would be greatly appreciated.
You gave to the function two names names.arg=c("No","Yes")and you have three levels according to your legend and the colors defined.
So, you need a vector with three names.

New gplots update in R cannot find function distfun in heatmap.2

I have a bit of R-code to make a heatmap from a correlation matrix, which worked the last time I used it (prior to the 2013 Oct 17 update of gplots; after updating to R Version 3.0.2). This makes me think that something changed in the most recent gplots update, but I can not figure out what.
What used to present a nice plot now gives me this error:
" Error in hclustfun(distfun(x)) : could not find function "distfun" "
and won't plot anything. Below is the code to reproduce the plot (heavily commented as I was using it to teach an undergrad how to use heatmaps for a project). I tried adding the last line to explicitly set the functions, but it didn't help resolve the problem.
EDIT: I changed the last line of code to read:
,distfun =function(c) {as.dist(1-c,upper=FALSE)}, hclustfun=hclust)
and it worked. When I used just "dist=as.dist" I got a plot, but it wasn't sorted right, and several of the dendrogram branches didn't connect to the tree. Not sure what happened, or why this is working, but it appears to be.
Any help would be greatly appreciated.
Thanks in advance,
library(gplots)
set.seed(12345)
randData <- as.data.frame(matrix(rnorm(600),ncol=6))
randDataCorrs <- randData+(rnorm(600))
names(randDataCorrs) <- paste(names(randDataCorrs),"_c",sep="")
randDataExtra <- cbind(randData,randDataCorrs)
randDataExtraMatrix <- cor(randDataExtra)
heatmap.2(randDataExtraMatrix, # sets the correlation matrix to use
symm=TRUE, #tells whether it is symmetrical or not
main= "Correlation matrix\nof Random Data Cor", # Names plot
xlab= "x groups",ylab="", # Sets the x and y labels
scale="none", # Tells it not to scale the data
col=redblue(256),# Sets the colors (can be manual, see below)
trace="none", # tells it not to add a trace
symkey=TRUE,symbreaks=TRUE, # Tells it to keep things symmetric around 0
density.info = "none"#) # can be "histogram" if you want a hist of your corr values here
#,distfun=dist, hclustfun=hclust)
,distfun =function(c) {as.dist(1-c,upper=FALSE)}, hclustfun=hclust) # new last line
I had the same error, then I noticed that I had made a variable called dist, which is the default call for distfun= dist. I renamed the variable and then everything ran fine. You likely made the same error, as your new code is working since you have altered the default call of distfun.

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