Disable rCharts animations - r

I am using rCharts and shiny in order to plot a variety of datasets. These datasets are dynamic and may in some cases be very big (10 000+ points in some cases). In these cases displaying the plot takes a lot of time (40s+).
Is there a way to generate an rCharts plot without the animations. This should significantly reduce the creating and rendering time of the plot.
I have looked trough the documentation (ltle that there is), but haven't found a way to do it.
The only thing that i have found is https://gist.github.com/timelyportfolio/10184829
I am using Polycharts, NVD3 and HighCharts.
EDIT: For highCharts i can do p$tooltip(enabled = F) and p$chart(animation = F). But any additional help would be appreciated, especially for Polycharts and NVD3

I am not a heavy user of Polycharts and Highcharts, so if you could add an example it would be helpful.
For NVD3 the variable is called transitionDuration.
An example of a chart without animations would be as follows:
library(rCharts)
hair_eye = as.data.frame(HairEyeColor)
p2 <- nPlot(Freq ~ Hair, group = 'Eye',
data = subset(hair_eye, Sex == "Female"),
type = 'multiBarChart')
p2$chart(transitionDuration=0)
p2

Related

How do you save a Highcharter chart as a gif in R?

I work with financial data that is mostly non-continuous, meaning market closing times and weekends need to be omitted from graphs.
Highcharter seems good at handling such data but how do you convert its charts into gifs? I know how to do this with ggplot and gganimate, but that combination does not handle financial data well.
Below is a simple line chart (of 10-year Treasury yields) done in Highcharter but I'd like to animate it and save it as a gif. Any help would be appreciated.
#load packages -------
library(here)
library(tidyverse)
library(highcharter)
# read in data -------
unemployment <- read_csv(here("data", "US 10-year Treasury.csv"))
# Line chart
data <- data.frame(unemployment)
hc <- data %>% hchart(
'line', hcaes(x = Date, y = Yield),
color = "steelblue"
)
hc
This functionality is not offered in the standard Highcharts library, therefore it has not been implemented in the Highcharter wrapper. Maybe R has some built-in tools for that gif export, unfortunately, I have no experience with it.

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
)

rCharts nvd3 facets not working

I'm trying to produce multiple charts based on different values of a specific variable in rCharts, ideally using NVD3 (nPlot). I believe this should be implementable using facets. However, I am unable to even get the example provided by rCharts to work:
p13 <- nPlot(mpg ~ wt, data = mtcars, group = "gear", type = "scatterChart")
p13$params$facet = "cyl"
p13$templates$script = system.file(
"/libraries/nvd3/layouts/nvd3FacetPlot.html",
package = "rCharts"
)
p13
This doesn't produce a chart for me. When I remove the p13$templates$script line, I get a single chart, no different from when I had not set a facet parameter. Am I missing something? I'm confident if I can simply reproduce this code to make it work I can get my own chart to work. I've installed the latest version of rCharts as of today (2/28/16).
As a separate question (if that's allowable), how do I add a title to an nPlot?
nvd3's help shows "facet has not been supported yet"
you can try the package : plotly ,it can facets well,and also have interactive effect

ggplot2 equivalent of 'factorization or categorization' in googleVis in R

Due to static graph prepared by ggplot, we are shifting our graphs to googleVis with interactive charts. But when it comes to categorization we are facing many problems. Let me give example which will help you understand:
#dataframe
df = data.frame( x = sample(1:100), y = sample(1:100), cat = sample(c('a','b','c'), 100, replace=TRUE) )
ggplot2 provides parameter like alpha, colour, linetype, size which we can use with categories like shown below:
ggplot(df) + geom_line(aes(x = x, y = y, colour = cat))
Not just line chart, but majority of ggplot2 graphs provide categorization based on column values. Now I would like to do the same in googleVis, based on value df$cat I would like parameters to get changed or grouping of line or charts.
Note:
I have already tried dcast to make multiple columns based on category column and use those multiple columns as Y input, but that it not what I would like to do.
Can anyone help me regarding this?
Let me know if you need more information.
vrajs5 you are not alone! We struggled with this issue. In our case we wanted to fill bar charts like in ggplot. This is the solution. You need to add specifically named columns, linked to your variables, to your data table for googleVis to pick up.
In my fill example, these are called roles, but once you see my syntax you can abstract it to annotations and other cool features. Google has them all documented here (check out superheroes example!) but it was not obvious how it applied to r.
#mages has this documented on this webpage, which shows features not in demo(googleVis):
http://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html
EXAMPLE ADDING NEW DIMENSIONS TO GOOGLEVIS CHARTS
# in this case
# How do we fill a bar chart showing bars depend on another variable?
# We wanted to show C in a different fill to other assets
suppressPackageStartupMessages(library(googleVis))
library(data.table) # You can use data frames if you don't like DT
test.dt = data.table(px = c("A","B","C"), py = c(1,4,9),
"py.style" = c('silver', 'silver', 'gold'))
# Add your modifier to your chart as a new variable e.g. py1.style
test <-gvisBarChart(test.dt,
xvar = "px",
yvar = c("py", "py.style"),
options = list(legend = 'none'))
plot(test)
We have shown py.style deterministically here, but you could code it to be dependent on your categories.
The secret is myvar.googleVis_thing_youneed linking the variable myvar to the googleVis feature.
RESULT BEFORE FILL (yvar = "py")
RESULT AFTER FILL (yvar = c("py", "py.style"))
Take a look at mages examples (code also on Github) and you will have cracked the "categorization based on column values" issue.

R rCharts - a slider not appearing when x-axis values are the dates

I am building a rCharts object. It uses a vector of strings formatted like a data ("yyyy-mm-dd") as values of x-axis. This way is taken from this rCharts 'official' tutorial (find m1 plot code) - in both (tutorial's and mine) cases the x-axis values are of character type and rCharts seems to recognize it (for example it sets the x-axis visible values to "yyyy-mm" format when the scope is narrow enough etc.)
For my purposes, it would be absolutely great if I could use the slider in the x-axis, but my implementation fails to work... The slider simply does not appear at all. Below is a short R code with such not-working-with-slider plot. Any solutions?
library(rCharts)
set.seed(101)
dates <- c("1959-01-01", "1959-02-01", "1959-03-01", "1959-04-01", "1959-05-01", "1959-06-01",
"1959-07-01", "1959-08-01", "1959-09-01", "1959-10-01", "1959-11-01", "1959-12-01",
"1960-01-01", "1960-02-01", "1960-03-01", "1960-04-01", "1960-05-01", "1960-06-01",
"1960-07-01", "1960-08-01", "1960-09-01", "1960-10-01", "1960-11-01", "1960-12-01")
data1 <- rnorm(n=24, m=1, sd=1)
data2 <- rnorm(n=24, m=0, sd=2)
df <- data.frame(dates, data1, data2)
plot <- mPlot(x = "dates", y = c("data1", "data2"), type = "Line", data = df)
plot$set(slider=TRUE)
plot
Posting my comment as an answer. mPlot uses the MorrisJS library, which does not have native support for sliders built in. If you look at the examples, you will notice that the slider method is supported by the JS library Rickshaw.

Resources