Set title in dPlot / R - r

I'm trying to set a title in my dPlot using rCharts:
library(rCharts)
a <- data.frame(dat=0,count=0)
t <- dPlot(
x = "dat",
y = "count",
data = a,
type = "line",
height = 400,
width = 700,
bounds = list(x=50,y=20,width=650,height=300)
)
t$templates$script = "http://timelyportfolio.github.io/rCharts_dimple/chartWithTitle.html"
t$set( title = "No Data found" )
t
Unfortunately, the suggested work around here doesn't work. Can someone help? Thanks.

Related

R Apexcharter: Formatting tooltip

I created an areaRange plot with the dreamRs apexcharter package and have a few issues formatting the hoverlabel/tooltip.
This is my sample code:
First, I installed the dreamRs apexcharter version using this:
#install.packages("remotes")
#remotes::install_github("dreamRs/apexcharter")
And then I loaded the following packages:
library(dplyr)
library(apexcharter)
The apexcharter version I have now is: apexcharter_0.3.1.9200
This is my example data:
test_data <- data.frame(seq(as.POSIXct('2022/09/04 22:00:00'), as.POSIXct('2022/09/08 10:00:00'), by="hour"))
test_data$MIN <- runif(n = 85, min = 70, max = 100)
test_data$MEDIAN <- runif(n = 85, min = 100, max = 120)
test_data$MAX <- runif(n = 85, min = 120, max = 150)
colnames(test_data) <- c("Date", "MIN", "MEDIAN", "MAX")
And this is my plot so far:
axc_plot <- apex(data = test_data, # plot the area range
mapping = aes(x = test_data[20:60,]$Date,
ymin = test_data[20:60,]$MIN,
ymax = rev(test_data[20:60,]$MAX)),
type = "rangeArea",
serie_name = "Vertrauensbereich") %>%
add_line(mapping = aes(x = Date, y = MEDIAN), # add the line
type = "line",
serie_name = "Median") %>%
ax_colors("lightblue", "red") %>% # why is the line not red?
ax_labs(x = "Zeit [h]",
y = "Q [m³/s]") %>%
ax_tooltip(enabled = T,
shared = T, # I want it shared but it's not
x = list(format = "dd.MM. HH:mm"), # changes grey hoverlabel at the bottom -> works
y = list(formatter = JS("function(seriesName) {return seriesName;}"), # instead of the time I want it to say "Median" and "Vertrauensbereich"
title = list(formatter = JS("function(test_data$Date) {return test_data$Date;}")))) # the title of the hoverlabel should be the time in the format "yyyy-MM-dd HH:mm:ss"
axc_plot
Here's how it looks:
rangeArea Plot with tooltip
As you can see the data in the tooltip is not displayed very well, so I want to format it using ax_tooltip but that hasn't worked very well so far. I found out that using x = will change the grey hoverlabel at the bottom of the plot and y = changes the label that runs along with the lines (which is the one I want to change). I tried to make a custom tooltip using formatter = but I don't really know how to work with it because all examples I see are made with Java Script and I don't know how to implement that in R. In ax_tooltip(y = ...) you can see how I tried to change the format using JS() because I saw it once somewhere (can't find the link anymore sadly) but I'm pretty sure that's not the way to do it as it doesn't change anything.
In the end, I'd like to achieve a tooltip that looks something like this with the Date at the top (as title) in the format "yyyy-MM-dd HH:mm:ss" if possible and then the series names with the corresponding values and hopefully also with the unit m³/s:
apex desired tooltip
Thanks in advance for any answers. I'm looking forward to hearing your suggestions!
I also asked this question on GitHub where pvictor solved my problem perfectly. This is what they answered and what works for me:
library(htmltools)
test_data <- data.frame(seq(as.POSIXct('2022/09/04 22:00:00'), as.POSIXct('2022/09/08 10:00:00'), by="hour"))
test_data$MIN <- runif(n = 85, min = 70, max = 100)
test_data$MEDIAN <- runif(n = 85, min = 100, max = 120)
test_data$MAX <- runif(n = 85, min = 120, max = 150)
colnames(test_data) <- c("Date", "MIN", "MEDIAN", "MAX")
# explicit NA if not used in area range
test_data$MIN[-c(20:60)] <- NA
test_data$MAX[-c(20:60)] <- NA
# Construct tooltip with HTML tags
test_data$tooltip <- unlist(lapply(
X = seq_len(nrow(test_data)),
FUN = function(i) {
d <- test_data[i, ]
doRenderTags(tags$div(
style = css(padding = "5px 10px;", border = "1px solid #FFF", borderRadius = "5px"),
format(d$Date, format = "%Y/%m/%d %H:%M"),
tags$br(),
tags$span("Q Median:", tags$b(round(d$MEDIAN), "m\u00b3/s")),
if (!is.na(d$MIN)) {
tagList(
tags$br(),
tags$span("Vertrauensbereich:", tags$b(round(d$MIN), "m\u00b3/s -", round(d$MAX), "m\u00b3/s"))
)
}
))
}
))
axc_plot <- apex(
data = test_data[20:60, ], # plot the area range
mapping = aes(
x = Date,
ymin = MIN,
ymax = rev(MAX),
tooltip = tooltip # variable containing the HTML tooltip
),
type = "rangeArea",
serie_name = "Vertrauensbereich"
) %>%
add_line(
data = test_data,
mapping = aes(x = Date, y = MEDIAN, tooltip = tooltip), # use same tooltip variable
type = "line",
serie_name = "Median"
) %>%
ax_colors(c("lightblue", "#FF0000")) %>% # use HEX code instaed of name
ax_theme(mode = "dark") %>%
ax_labs(
x = "Zeit [h]",
y = "Q [m³/s]"
) %>%
ax_tooltip(
# Custom tooltip: retrieve the HTML tooltip defined in data
custom = JS(
"function({series, seriesIndex, dataPointIndex, w}) {",
"var tooltip = w.config.series[seriesIndex].data[dataPointIndex].tooltip;",
"return typeof tooltip == 'undefined' ? null : tooltip;",
"}"
)
)
axc_plot
You can find the GitHub entry here: https://github.com/dreamRs/apexcharter/issues/62

Adding title to simple_visNet

I am using simple_visNet to represent data but I am not able to add a title to my graph. Any idea?
simu_data_mapper <- mapper.sta(dat = data,
filter_values = filt,
num_intervals = 10,
percent_overlap = 70)
simple_visNet(simu_data_mapper, color_filter = FALSE)

Remove legend from rChart

I am using rCharts library in a Shiny application and want to remove the right side legend.
The tips I found did not work in this case...
for example: p1$chart(showLegend = FALSE)
My code is similar to this one:
library(rCharts)
mydata<-data.frame(weekday=c("friday","monday","thursday","tuesday","saturday","sunday","wednesday"),value=rnorm(7,10,2))
p1<-rPlot(value ~ weekday, color = 'weekday', data = mydata, type = 'bar')
p1$guides(
color = list(
numticks = length(unique(mydata$weekday))
),
x = list(title="",
ticks = c('sunday', 'monday', 'tuesday','wednesday','thursday','friday','saturday'),
labels ="",
levels = c('sunday', 'monday', 'tuesday','wednesday','thursday','friday','saturday')
),
y = list(title=""
)
)
p1$chart(showLegend = FALSE)
p1
I solved the problem using the following code line:
p1$set(legendPosition = "none")

Rcharts Shiny : not reload all variables after just one variable update

i've this kind of shiny Highcharts function in shiny
h1 <- Highcharts$new()
h1$series(data = test$sites, type="column",name = "Sites Actifs",enabled=T)
h1$series(data = test$alerte, type="column",name = "Sites Alerte")
h1$series(data = test$ID, type="area",name = "CAID", enabled=F)
h1$series(data = test$LD, type="area",name = "MILD")
h1$series(data = test$rat, type="line", name = "Percentile")
h1$series(data = test$epi, type="line", name = "Outbreak")
h1$series(data = test$Temp, type='line', name = "Temperatures")
h1$series(data = test$ND, type = "line", name = "NDVI")
h1$series(data = test$Pm, type="line",name = "Pmm",enabled=T)
h1$colors('rgba(187, 165, 203, 0.9)', 'rgba(187, 40, 175,0.9)','rgba(255,179,128,.9)','rgba(0,0,128,.4)','rgba(170,0,0, .9)', 'rgba(0,0,0, .9)', 'rgba(243,170,80, .9)','rgba(120,193,102, .9)','rgba(85,153,255,.9)')
h1$legend(symbolWidth = 30)
h1$plotOptions(line=list(marker=list(enabled = F)),area=list(marker=list(enabled = F)))
h1$chart(zoomType = "x1")
h1$exporting(enabled = T)
h1$params$width <- 500
h1$params$height <- 700
h1$xAxis(type='datetime', categories=ref_time, tickInterval = 30)
return(h1)
But when i hide some variable (i.e. $sites, $alerte, $Temp) in order to test some hypothesis, if i update another variable (i.e. $rat) all the graph is updated and hidden variables are unhidden.
Do you have a solution to circumvent this "problem"
Thanks
I'm also not sure I understand your problem, but say you define a couple of checkboxes in your shiny app that allow you to hide/show a specific series all you need to do is use that with the visible option, e.g.
h1$series(data = test$ND, type = "line", name = "NDVI", visible=input$show.NDVI)
The plot will still be generated each time you change a checkbox, but the hidden series will stay the same. Another option might be not to add the series in the first place, e.g.
if(input$show.NDVI){
h1$series(data = test$ND, type = "line", name = "NDVI", visible=input$show.NDVI)
}

rcharts dimple bubble chart in shiny

Using a variation of the below example dimple chart, how can I get this to auto scale with Shiny bootstrap without having to hard code height and width of the chart?
#get data used by dimple for all of its examples as a first test
data <- read.delim(
"http://pmsi-alignalytics.github.io/dimple/data/example_data.tsv"
)
#eliminate . to avoid confusion in javascript
colnames(data) <- gsub("[.]","",colnames(data))
#example 27 Bubble Matrix
d1 <- dPlot(
x = c( "Channel", "PriceTier"),
y = "Owner",
z = "Distribution",
groups = "PriceTier",
data = data,
type = "bubble",
aggregate = "dimple.aggregateMethod.max"
)
d1$xAxis( type = "addCategoryAxis" )
d1$yAxis( type = "addCategoryAxis" )
d1$zAxis( type = "addMeasureAxis", overrideMax = 200 )
d1$legend(
x = 200,
y = 10,
width = 500,
height = 20,
horizontalAlign = "right"
)
d1
Hi you have to put width="100%" in dplot(), like this :
d1 <- dPlot(
x = c( "Channel", "PriceTier"),
y = "Owner",
z = "Distribution",
groups = "PriceTier",
data = data,
type = "bubble",
aggregate = "dimple.aggregateMethod.max",
width="100%"
)

Resources