r circlize: missing value where TRUE/FALSE needed - r

I am trying to plot (for the first time) a chord diagram in the package circlize in R Studio. I am going through the manual chapters (Circular Visualization in R). The first step is to allocate the sectors on a circle by using the circos.initialize command. However, when I get to this step, I get an error stating missing values where TRUE/FALSE needed.
A reproducible example
library(circlize)
Types <- data.frame(Types = c("OOP", "UVA", "MAT", "OIC", "FIN", "WSE"))
stack.df <- data.frame(Year = c(rep(2019, 1), rep(2020, 4), rep(2021, 7), rep(2022, 11), rep(2023, 11)), Invoice = c(paste0("2019.", "10", ".INV"),
paste0("2020.", seq(from = 20, to = 23, by = 1), ".INV"),
paste0("2021.", seq(from = 30, to = 36, by = 1), ".INV"),
paste0("2022.", seq(from = 40, to = 50, by = 1), ".INV"),
paste0("2023.", seq(from = 50, to = 60, by = 1), ".INV")))
stack.df <- cbind(stack.df, Org_1 = Types[sample(nrow(Types), nrow(stack.df), replace = TRUE), ], Org_2 = Types[sample(nrow(Types), nrow(stack.df), replace = TRUE), ])
Making Chord Diagram
My overall objective: Make a chord diagram where the sectors are the stack.df$Year and track 1 is the stack.df$Invoice, with the circos.links from stack.df$Org_1 to stack.df$Org_2.
Initialize
circos.initialize(sectors = stack.df$Year, x = stack.df$Invoice)
Error in if (sector.range[i] == 0) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In circos.initialize(sectors = stack.df$Year, x = stack.df$Invoice) :
NAs introduced by coercion
What am I am missing? My sector.range !== 0 as stack.df$Year is from 2019-2023. Any help in overcoming this error is greatly appreciated.

Related

meteoForecast for precipitation from GFS: : Point outside GFS-MG region

Error with getPoint function (GFS) for location (lon = 29.7, lat = -0.9) and variable ("Precipitable_water_entire_atmosphere_single_layer") : Point outside GFS-MG region.
See the script below:
library(meteoForecast)
list<-grepVar('prec', service = 'gfs', complete = TRUE)
list$name
list$label
list$name
testDay <- Sys.Date() - 1
cloudGFS <- getRaster(list$name[1],
day = testDay,
box = c(-30, 30, 30, 50),
service = 'gfs')
EACrain <- getRaster(list$name[1],
day = testDay,
box = c(12, 32, -10, 10),
service = 'gfs')
#levelplot(cloudGFS, layout = c(1, 1))
plot(EACrain, layout = c(1, 1))
rainpoint <- getPoint(c(29.7, -0.9), vars = list$name[1],
day = testDay, service = 'gfs')

A calendar issues in wavelet analyze

I want to apply a simple wavelet analyze using "waveletcomp" package. I want to use the year shown in x-axis. But it always report error in "lease check your calendar dates, format and time zone: dates may not be in an unambiguous format or chronological. The default numerical axis was used instead." I tried to fix the date, but it seems fine. I really don't know where is the wrong part. Thank you in advance.
Here is the code.
library('WaveletComp')
firecount <- data.frame( YEAR = c("1986-01-01","1987-01-01","1988-01-01","1989-01-01","1990-01-01"
,"1991-01-01","1992-01-01","1993-01-01","1994-01-01","1995-01-01"
,"1996-01-01","1997-01-01","1998-01-01","1999-01-01","2000-01-01"
,"2001-01-01","2002-01-01","2003-01-01","2004-01-01","2005-01-01"
,"2006-01-01","2007-01-01","2008-01-01","2009-01-01","2010-01-01"
,"2011-01-01","2012-01-01","2013-01-01","2014-01-01","2015-01-01"
,"2016-01-01","2017-01-01","2018-01-01","2019-01-01","2020-01-01"
),
COUNT = c(3,5,4,0,0,0,13,0,2,3,0,1,0,3,15,13,
59,18,42,16,20,46,44,8,68,18,7,3,9
,48,7,48,23,84,54)
)
flycount$YEAR <- as.Date(as.character(firecount$YEAR),"%Y")
my.w <- analyze.wavelet(flycount, my.series = "COUNT",
loess.span = 0.5,
dt = 1, dj = 1/35,
lowerPeriod = 2, upperPeriod = 12,
make.pval = TRUE, n.sim = 10,
)
wt.image(my.w, color.key = "interval", n.levels = 15,
legend.params = list(lab = "fire occurrence wavelet", label.digits = 2),
periodlab = "periods (years)",
# Concerning item 1 above --- plot the square root of power:
exponent = 0.5,
# Concerning item 2 above --- time axis:
show.date = TRUE,
date.format = "%F",
timelab = "",
spec.time.axis = list(at = c(paste(1986:2020, "-01-01", sep = "")),
labels = c(1986:2020)),
timetcl = -0.5)
The function analyze.wavelet automatically takes the date from a dataframe column called date. So just rename your column from YEAR to date and you're good to go.

Error while running WTC (Wavelet Coherence) Codes in R

I am doing Wavelet Analysis in R using Biwavelet. However, I receive the error message:
Error in check.datum(y) :
The step size must be constant (see approx function to interpolate)
When I run the following code:
wtc.AB = wtc(t1, t2, nrands = nrands)
Please share your help here. Complete Code is:
# Import your data
Data <- read.csv("https://dl.dropboxusercontent.com/u/18255955/Tutorials/Commodities.csv")
# Attach your data so that you can access variables directly using their
# names
attach(Data)
# Define two sets of variables with time stamps
t1 = cbind(DATE, ISLX)
t2 = cbind(DATE, GOLD)
# Specify the number of iterations. The more, the better (>1000). For the
# purpose of this tutorial, we just set it = 10
nrands = 10
wtc.AB = wtc(t1, t2, nrands = nrands)
# Plotting a graph
par(oma = c(0, 0, 0, 1), mar = c(5, 4, 5, 5) + 0.1)
plot(wtc.AB, plot.phase = TRUE, lty.coi = 1, col.coi = "grey", lwd.coi = 2,
lwd.sig = 2, arrow.lwd = 0.03, arrow.len = 0.12, ylab = "Scale", xlab = "Period",
plot.cb = TRUE, main = "Wavelet Coherence: A vs B")```

Errors adding genomic feature track in circlize

I'm trying to generate a circos plot with a simple genomic notation from BED files. However, when I use circos.genomeRect this results in an error, or in a track that does not plot rectangles, but semicircles as I show below.
Consider the following reproducible example:
library("circlize")
library("tidyverse")
circos.par(start.degree = 90,
cell.padding = c(0, 0, 0, 0),
#points.overflow.warning=FALSE,
track.height = 0.10
)
# Initialize genome (bed file with genome sizes)
genome <- tibble(chr=c("chr1","chr2"), start = c(1,1), end = c(6000000, 3000000))
circos.genomicInitialize(genome, plotType = c("axis"), major.by = 1000000)
# Add track with annotation
feature <- tibble(chr = c("chr1", "chr1"), start = c(2500, 4500000), end = c(4150000, 6350000))
circos.genomicTrack(feature, ylim=c(0,1),
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, ytop.column = 1, ybottom = 0, col="blue")
})
circos.clear()
This returns an error:
Error in if (sum(l) && circos.par("points.overflow.warning")) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In is.na(x) | is.na(y) :
Error in if (sum(l) && circos.par("points.overflow.warning")) { :
missing value where TRUE/FALSE needed
At this point, if points.overflow.warning=FALSE is set in circos.par above, the error disappears, but some other error must be occurring, that this does not plot rectangles:
Am I missing something? what is wrong with this simple example? Thank you
EDIT
I just noticed that the feature dataframe I plot has one coordinate wrong, since it extends longer than the actual size of the chromosome. However, if this is fixed, eg: feature <- tibble(chr = c("chr1", "chr1"), start = c(2500, 4500000), end = c(4150000, 5350000)), a new error appears!!
Warning message:
In is.na(x) | is.na(y) :
longer object length is not a multiple of shorter object length
It seems to work with data.frames instead of tibbles:
library("circlize")
circos.par(start.degree = 90,
cell.padding = c(0, 0, 0, 0),
#points.overflow.warning=FALSE,
track.height = 0.10
)
# Initialize genome (bed file with genome sizes)
genome <- data.frame(chr=c("chr1","chr2"), start = c(1,1), end = c(6000000, 3000000))
circos.genomicInitialize(genome, plotType = c("axis"), major.by = 1000000)
# Add track with annotation
feature <- data.frame(chr = c("chr1", "chr1"), start = c(2500, 4500000), end = c(4150000, 5350000))
circos.genomicTrack(feature, ylim=c(0,1),
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, col="blue")
})
circos.clear()
Created on 2020-08-11 by the reprex package (v0.3.0)

ddply to ksmooth function

I have a data frame with several columns. the relevant three are chr, pos and ratio. I want to use ddply to ksmooth based on chr (chromosome) but keep getting a wrong data frame with lots of NA values. Here is my reproducible data frame:
d=data.frame(chr=c(rep.int(1,24),rep.int(2,15),rep.int(3,30),rep.int(4,20),rep.int(5,11)),
pos=c(sort(sample(1:1000, size = 24, replace = FALSE),decreasing = FALSE), sort(sample(1:1000, size = 15, replace = FALSE),decreasing = FALSE), sort(sample(1:1000, size = 30, replace = FALSE),decreasing = FALSE), sort(sample(1:1000, size = 20, replace = FALSE),decreasing = FALSE), sort(sample(1:1000, size = 11, replace = FALSE),decreasing = FALSE)),
ratio=seq(1:100))
and ddply function
f <- ddply(d, .(chr),
function(e) {
as.data.frame(ksmooth(e$pos,e$ratio,"normal",bandwidth=10))
})
Obviously I'm doing something wrong.
Thanks for the help,
Guy
This is nothing related to plyr::ddply. The issue is with ksmooth. You want:
ksmooth(e$pos, e$ratio, "normal", bandwidth=10, x.points = e$pos)
Read ?ksmooth for what x.points means. By default, this is NULL, and ksmooth will use n.points instead. This is the source of all your trouble.

Resources