Heatmap with calendR - r

Newbie here, I am having an issue creating a heatmap with the following data (365 variables)
The error is:
"Error in calendR(year = 2021, special.days = Events11, gradient = TRUE, :
No element of the 'special.days' vector can be greater than the number of days of the corresponding month or year"
The only time it seems to work is when "special.days = 1:365" for some reason, can anyone help?
Events11 <- c(879,1997,2122,2767,3334,3425,3373,3353,4352,2882,2964,3686,3922,3152,3536,2819,2110,3101,2687,2290,3678,2658,3004,2586,2586,1089,1922,2254,2769,1844,368,1516,1973,2292,1430,1064,663,521,844,1176,1112,1163,1544,582,459,312,221,494,645,782,907,737,1275,2485,2821,2829,3515,6612,3022,3010,3647,5073,3579,4840,6149,5813,8150,9511,6704,6938,6499,11675,4833,1952,3797,3342,3275,5974,11779,15132,10208,4661,7376,5427,5538,11605,6663,8053,8828,5658,4812,8217,17934,15919,10248,15543,14057,4645,11047,4764,9813,9884,8937,7373,7613,9728,12547,11891,5883,5561,5671,9492,11812,13328,7717,12754,16214,5780,8443,10576,21690,22206,8531,7884,10692,8006,11233,12929,6071,7876,9380,11428,14119,17265,13049,20364,12795,10549,13406,17304,20889,30104,18677,16910,15846,17105,9676,5682,18111,22761,18455,16354,17081,19631,21521,30320,24527,14793,18815,20676,21281,24869,28841,29919,19366,20332,21539,21771,23542,31261,18387,17988,20929,19720,12373,13415,11694,24158,13381,14631,15659,20661,22326,28036,28086,23317,19473,19109,19031,24713,20944,10558,16633,19040,21134,17094,19541,31877,27352,19866,21586,21290,21986,21712,27321,26976,20321,21349,22253,22841,24917,30966,25601,19443,20559,21065,21927,22585,30466,23038,14026,17085,17844,20310,24250,32693,26920,19540,20916,21128,21063,23518,26588,25821,19457,14416,16899,21696,22544,25713,19766,18540,18968,20954,20616,19491,23345,28157,24786,15904,20167,20770,22116,28073,22788,17766,19359,20847,22395,21727,27347,23476,15968,14458,13638,16652,19825,24884,21715,17256,18275,19135,19228,21720,19814,15708,14377,16425,16943,13247,16907,27027,25335,10716,15976,14720,16662,16007,20841,19649,16428,18308,17843,13039,14957,17318,3700,6407,13638,14054,7826,4673,14996,12711,10619,10775,10815,10800,11542,13998,13985,12741,12248,12649,8324,6884,7688,4925,7070,10035,8261,7648,8192,9358,7975,6196,6926,6387,2785,3155,4468,4491,6375,8552,8766,10617,9641,8539,3107,5466,5286,6626,7755,5675,5902,6524,7365,8356,9047,6872,6544,4375,4227,5613,5041,3697,4320,3224,2266,2733,3521,2042,3931,4604,4689)
calendR(year = 2021,
special.days = Events11,
gradient = TRUE,
special.col = rgb(1, 0, 0, alpha = 0.6),
low.col = "white")

"Error in calendR(year = 2021, special.days = Events11, gradient = TRUE, :
No element of the 'special.days' vector can be greater than the number of days of the corresponding month or year"
This is due to the way calendR do with scale - they restricted the values of element to no greater than 365. What you can do is normalize the input to a scale that in acceptable range no greater than 365 for a year or 3x days for a month.
Here is an example
library(calendR)
# a scale function that always have the value between 0 - 1
scale_fn <- function(x) { x / sqrt(sum(x^2)) }
# normalized your data
Events11_normalized <- scale_fn(Events11)
calendR(year = 2021,
special.days = Events11_normalized,
gradient = TRUE,
special.col = rgb(1, 0, 0, alpha = 0.6),
low.col = "white")
And here the calendR work just fine.
Created on 2022-01-26 by the reprex package (v2.0.1)

Related

Holt-Winters fitted values does not start from first date in R

I am trying to overlay Holt-Winter fitted values (red) with original values (black) in R, but the red line does not begin from the start.
Perhaps this is related to the size of moving window, if yes, how do I alter in the HoltWinter() object in R?
Tried:
dfts <- dfts$Monthly_Sales %>% ts(start = c(2019, 1), end = c(2021, 12), frequency = 12)
hw1 <- HoltWinters(dfts)
hw1.pred <- predict(hw1, n.ahead = 24, prediction.interval = TRUE, level = 0.95)
plot(hw1, predicted.values = hw1.pred, ylab = "Sales Amt")
Expecting: Red line to begin from the start or near to the start.
Got:
Thanks.

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.

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)

How can i have more than three gauge sectors in flexdashboard?

Flexdashboard allows to specify three sectors for its gauges: "danger", "warning" and "success". I want to use 5 gauge sectors to show in which interval my observed value lies. I calculated confidence intervals with alpha 0.2 (80 %) and 0.01 (99 %) and use this to define 5 sectors:
Sector 1 = c(min(value),lower_90_ci)
Sector 2 = c(lower_90_ci,lower_80_ci)
Sector 3 = c(lower_80_ci, upper_80_ci)
Sector 4 = c(upper_80_ci, upper_90_ci)
Sector 5 = c(upper_90_ci, max(value))
This is a standard-gauge in flexdashboard:
library(flexdashboard)
gauge(42, min = 0, max = 100, symbol = '%', gaugeSectors(
success = c(80, 100), warning = c(40, 79), danger = c(0, 39)
))
If the intention is to have optimum range in the middle with warning and danger on both higher and lower sides, I tried this:
gauge(value = 95, # For example
min = 0,
max = 100,
sectors = gaugeSectors(
success = c(20, 80),
warning = c(10, 90),
danger = c(0, 100)
)
)
You may want to make sure that the sectors covers the whole range (min-max). Any value within the range but not belonging to any sector will use default color (success).
I don't think it can be done out of the box. Digging into the resolveSectors function shows that it expects three sectors and is quite inflexible:
function (sectors, min, max)
{
if (is.null(sectors)) {
sectors = sectors(success = c(min, max), warning = NULL,
danger = NULL, colors = c("success", "warning", "danger"))
}
if (is.null(sectors$success) && is.null(sectors$warning) &&
is.null(sectors$danger)) {
sectors$success <- c(min, max)
}
if (is.null(sectors$colors))
sectors$colors <- c("success", "warning", "danger")
customSectors <- list()
addSector <- function(sector, color) {
if (!is.null(sector)) {
if (!is.numeric(sector) || length(sector) != 2)
stop("sectors must be numeric vectors of length 2",
call. = FALSE)
customSectors[[length(customSectors) + 1]] <<- list(lo = sector[[1]],
hi = sector[[2]], color = color)
}
}
sectors$colors <- rep_len(sectors$colors, 3)
addSector(sectors$success, sectors$colors[[1]])
addSector(sectors$warning, sectors$colors[[2]])
addSector(sectors$danger, sectors$colors[[3]])
customSectors
}
<environment: namespace:flexdashboard>
Nevertheless, you could build your own gauge function that uses a custom built resolveSectors function (using the current function as a template) that expects five sectors.

R flexdashboard gauge function won't change color

I'm trying to make a gauge with a blue color using the flexdashboard package in R, however I can't seem to change the color of the gauge. It seems that it only comes in three preset colors of success, warning, and danger. My problem is that I can't seem to change success to blue. Here's my code
library(flexdashboard)
gauge(20,
min = 0,
max = 100,
symbol = "%",
sectors = gaugeSectors(success = c(0, 0.4),
warning = c(0.4, 0.6),
danger = c(0.6, 1)
) )
You can modify colors via the colors argument in the gaugeSectors function. As the help states ?gaugeSectors:
Colors can be standard theme colors ("success", "warning", "danger",
"primary", and "info") or any other valid CSS color specifier
So you need to add a colors argument like this: colors = c("blue", rgb(0, 1, 0), "#CC664D")
And also you made a mistake while specifying the value ranges in gaugeSectors: your minimum and maximum values are 0 and 100, so you need to provide values within this range:
success = c(0, 40),
warning = c(40, 60),
danger = c(60, 100)
Please note that the symbol "%" doesn't actually convert the value to percentage, it is just a string that is printed after the value.
gauge(20,
min = 0,
max = 100,
symbol = "%",
sectors = gaugeSectors(success = c(0, 40),
warning = c(40, 60),
danger = c(60, 100),
colors = c("blue", rgb(0, 1, 0), "#CC664D")
)
)

Resources