ggvis slider for bar plots - r

Am using R version 3.2.2. I am trying to create a bar plot with date on the x axis with a slider to control the number of date values displayed. I cannot figure what option helps me do this. I tried:
library(dplyr)
library(ggvis)
library(shiny)
shinyServer(function(input,output){
final_data %>%
ggvis(~Date, ~work_left) %>%
layer_bars(width = input_slider(min = 1, max = 10)) %>%
along with a host of other options including size which I found on other help sites. But none of them work.
My ui.r is a simple display of the chart in the main panel.
Any help is appreciated. Thanks

Related

R crosstalk filter_select: cannot unselect already selected value

I am trying to use crosstalk to filter a plotly chart. I use crosstalk's filter_select to (un)filter a plotly chart. When using the reprex below trying to select a flower (e.g. setosa), all works fine. But when I unselect the flower, the values are not 'returned' to the plotly chart. Selecting '(All)' does not seem to be working either. The values reappear only when deselecting from flower filter AND ticking TRUE in the helper_select_all.
library(crosstalk)
library(plotly)
df <- iris
df$helper_select_all <- T
shared_data <- SharedData$new(df)
filter <- bscols(
list(
filter_select("flower", "Select a flower", shared_data, ~Species),
filter_checkbox(
"helper_select_all",
label = "Reset filtering as deselecting from 'Select a flower' is not working",
shared_data, ~helper_select_all)
)
)
p <- plot_ly(shared_data, x = ~Sepal.Length, y = ~Sepal.Width) %>%
add_markers()
bscols(filter, p)
This picture demonstrates the issue as no filtering value is selected, but a part of the data is missing.
This picture shows what it looks like when all datapoints are present.
"(All)" does not seem to be working either.

Remove value display from eCharts4R gauge chart

Given this example
library(echarts4r)
library(magrittr)
gauge_out <- e_charts() %>%
e_gauge(41,"Percent")
print(gauge_out)
You get this gauge chart
I'd like to NOT have the number "41" displayed at the bottom. From looking through the eCharts docs if there's a quick way to do that I seem to be missing it.
Got it...
library(echarts4r)
library(magrittr)
gauge_out <- e_charts() %>%
e_gauge(41,"Percent",
detail = list(
show = FALSE
))
print(gauge_out)

Is there a way to have a highlighted chart as well as have interactivity of selecting elements in R?

I have come across a beautiful chart on this webpage: https://ourworldindata.org/coronavirus and interested to know if we can build the same chart in R with functionality of having highlighted series as well as selecting any line on hovering ?
I have build static highlighted charts using gghighlight but those are not interactive.
Plotly can help in interaction but I think they don't work with gghighlight.
So how can we have the combination of both highlight and interactivity in charts as in the link shared on top ?
Is it possible to achieve same results in R ? It would be really helpful if someone could share an example or link that can help.
(UPDATE: May be I can manually highlight lines by creating a factor column instead of using gghighlight and then pass it to ggplotly but then can ggplotly or some other library provide similar results on hover ?)
(NOTE: Not looking for animation. Just need highlighted, hover over interactive chart)
Below is the snapshot of same chart hovered over US (This chart is also similar to the one shared in World Economic Forum many times.)
Using plotly you can use highlight() to achive this.
This is a slightly modified example from here:
library(plotly)
# load the `txhousing` dataset
data(txhousing, package = "ggplot2")
# declare `city` as the SQL 'query by' column
tx <- highlight_key(txhousing, ~city)
# initiate a plotly object
base <- plot_ly(tx, color = I("black")) %>%
group_by(city)
# create a time series of median house price
time_series <- base %>%
group_by(city) %>%
add_lines(x = ~date, y = ~median)
highlight(
time_series,
on = "plotly_hover",
selectize = FALSE,
dynamic = FALSE,
color = "red",
persistent = FALSE
)

Retrieve axis tick information from a plotly figure

I'm plotting a heatmap using R plotly:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
As you can see, plotly is not showing all y-axis ticks. My question is whether it is possible, and if so how, to retrieve the y-axis tick labels plotly selected to show?
I saved the plot object:
plotly.obj <- plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
And looked around and it seems that perhaps plotly.obj$x$layoutAttrs should store this information but it doesn't:
> plotly.obj$x$layoutAttrs
$`102ce55fd393e`
$`102ce55fd393e`$yaxis
$`102ce55fd393e`$yaxis$title
NULL
$`102ce55fd393e`$xaxis
$`102ce55fd393e`$xaxis$tickangle
[1] 90
$`102ce55fd393e`$xaxis$tickvals
[1] 10
$`102ce55fd393e`$xaxis$ticktext
[1] "X-Label"
Any idea?
I don't think you can get the ticks, that are finally rendered. But you can get all the levels of the y-axis, that ploty can choose from.
levels(plotly.obj$x$attrs$`2c4c148651ae`$y)
The ticks that are finally rendered are dynamically chosen and will adapt, depending on your plot size etc.
You can also check out the attributes with plotly_json():
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label")) %>%
plotly_json()
I got the answer from a github issue I posted on ropensci/plotly:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label")) %>%
htmlwidgets::onRender(
"function(el, x) {
alert(el._fullLayout.yaxis._vals.map(function(i) { return i.text; }));
}"
)
Will pop up a browser window with the tick labels.
The question now is if this can be saved/piped to an R variable or written to a file so it can be done automatically rather than interactively. That's going to be another post.

R create interactive plot with slider which width could be changed like in Google Finance (sizeable time-window)

R create interactive plot with slider which width could be changed, example below, I'm searching in ggvis R package, but other are also welcome :
Have a look at dygraphs and dyRangeSelector().
The dygraphs package is an R interface to the dygraphs JavaScript
charting library. It provides rich facilities for charting time-series
data in R
For more information and examples, have a look at dygraph's github.io:
install.packages("dygraphs")
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths) %>%
dySeries("mdeaths", label = "Male") %>%
dySeries("fdeaths", label = "Female") %>%
dyOptions(stackedGraph = TRUE) %>%
dyRangeSelector(height = 20)
Which gives:
Highcharts/Highstock it's another great tool for this kind of plots and there is an awesome API wrapper in R: http://jkunst.com/highcharter/

Resources