R error with gap.plot: origin must be supplied - r

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

Related

Using the QQ Plot functionality in ggplot

I'm brand new to R, and have a data frame with 8 columns that has daily changes in interest rates. I can plot QQ plots for data each of the 8 columns using the following code:
par(mfrow = c(2,4))
for(i in 1:length(column_names)){
qqnorm(deltaIR.df[,i],main = column_names[i], pch = 16, cex = .5)
qqline(deltaIR.df[,i],cex = .5)
}
I'd like now to use the stat_qq function in the ggplot2 package to do this more elegantly, but just can't get my arms around the syntax - I keep getting it wrong. Would someone kindly help me translate the above code to use ggplot and allow me to view my 8 QQ plots on one page with an appropriate header? Trying the obvious
ggplot(deltaIR.df) + stat_qq(sample = columns[i])
gets me only an error message
Warning: Ignoring unknown parameters: sample
Error: stat_qq requires the following missing aesthetics: sample
and adding in the aesthetics
ggplot(deltaIR.df, aes(column_names)) + stat_qq()
is no better. The error message just changes to
Error: Aesthetics must be either length 1 or the same as the data (5271)
In short, nothing I have done so far (even with Google's assistance) has got me closer to a solution. May I ask for guidance?

Trying to make a histogram with two variables and keep coming up with 'x' must be numeric

I am trying to make a good histogram with two variables, height, and free throw percent. I've​ imported the data using excel and used the hist function but keep coming up with 'x' must be numeric.
I can read the data and create a table.
I've tried hist(height$freethrow)
and hist(shortkings)
This is what my data looks like
Second part of my data
hist(shortkings)
Error in hist.default(shortkings) : 'x' must be numeric
hist(shortkings, xlab = Height, ylab = Freethrow, main = Freethrow)
Error in hist.default(shortkings, xlab = Height, ylab = Freethrow, main = Freethrow) :
'x' must be numeric
I would like to create a histogram that shows distribution.
What happens if you run class(shortkings$Height)
If you see that it's non-numeric, then you can do the following and re-run the hist() function
shortkings$Height <- as.integer(shortkings$Height)

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)

Forecast with data series with quantmod and forecast package

I'm new to ts, and xts object.
When dealing with time series data, I encountered the problem
require(quantmod)
require(forecast)
ticker <- "^GSPC"
getSymbols(ticker, src="yahoo", to = "2013-12-31")
prices <- GSPC[,6] # First get data using package quantmod
# then forecasting using package forecast
prices.ts <- as.ts(prices)
prices.ets <- ets(prices.ts)
prices.fore <- forecast(prices.ets, h=10)
# then plot
plot(prices.fore, xaxt = "n")
My problems are :
1 . When I tried to save the GSPC with date in a csv file. I searched and tried this
write.zoo((GSPC, file = "GSPC.csv", sep = ",", qmethod = "double"))
The error message: Error: unexpected ',' in "write.zoo((GSPC," , I checked the syntax, it seems to be correct, and I tried other combinations. All failed with the similar error message.
also I tried index(GSPC) to get the date.
and then cbind(index(GSPC), GSPC[, 6]). It also failed..
Error message: Error in merge.xts(..., all = all, fill = fill, suffixes = suffixes) :
dims [product 1762] do not match the length of object [3524]
but when I checked the length
> length(GSPC[,6])
[1] 1762
> length(index(GSPC))
[1] 1762
2 . the plot is like this
there's no x-lab and y- lab. I tried the methods of accepted answer posted here, . but failed.
Especially, I don't get the purpose of the following code. It seems to change the appearance of the plot, but it doesn't change the appearance at all. I don't know whether I lose some points.
a = seq(as.Date("2011-11-01"), by="weeks", length=11)
axis(1, at = decimal_date(a), labels = format(a, "%Y %b %d"), cex.axis=0.6)
abline(v = decimal_date(a), col='grey', lwd=0.5)
Also, I want to plot from as.Date("2013-01-01").
Could you please give some suggestions?
Thanks a lot!
You have additional parenthesis. Use
write.zoo(GSPC, file = "GSPC.csv", sep = ",", qmethod = "double")
I don't know what you are trying to achieve with your index and cbind commands. index does not give the data. And if you want the 6th column of GSPC just use GSPC[,6].
It looks like you have some non-standard plotting dimensions. Start a new graphics window and you will reset them to defaults. But you won't get xlab and ylab unless you specify them explicitly. And you won't get an x-axis because you have set xaxt="n"
The questions about the last code block do not seem to relate to your data at all.

error labelling axis of plot using Ecdf

I am attempting to plot a graph using the code below:
Require(Hmisc)
Ecdf(ceac_primary,xlab="axis label",xlim=c(5000,50000),q=c(0.9,0.1),
ylab="Probability of Success",main="CEAC")
Where ceac_primary is a data frame with 1 variable of 90k observations.
When I include the 'xlab="axis label"' I keep getting the following error:
Error in Ecdf.default(v, group = group, weights = weights, normwt = normwt, :
formal argument "xlab" matched by multiple actual arguments
However if I exclude the x axis label part of the code, it plots the graph fine.
Is this a known problem, and if so, are there alternative ways to plot an x axis label?
Thanks
Digging around in the source code for Ecdf.data.frame (the method that is called when passing a data.frame to Ecdf), it looks like that function creates an object that is later passed to the xlab argument. Therefore, xlab is not expected as a user-supplied argument when running Ecdf with a data.frame. Here's the code that creates the object lab that gets passed to xlab within Ecdf.data.frame:
lab <- if (vnames == "names")
nam[j]
else label(v, units = TRUE, plot = TRUE, default = nam[j])
Then Ecdf is called with xlab = lab, but also any arguments in the elipses of Ecdf.data.frame are also passed to Ecdf. Since xlab is not a formal argument of Ecdf.data.frame, this is why you get your error.
To get around it, try either of the following:
Convert your data.frame to a vector of the appropriate class (numeric, I presume), and then run
Ecdf(ceac_primary_Vec, xlab = "axis label")
Or, you can create a label for the one column in your data.frame using the label function in the Hmisc package. If that column is called myCol, you can run
label(ceac_primary$myCol) <- "axis label"
Ecdf(ceac_primary)
And that should get your axis label printing correctly.

Resources