Remove value display from eCharts4R gauge chart - r

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)

Related

Changing color of points in R Sentimentr plot() function

I am trying to change the color of the red points in the plot() function used by the Sentimentr package. I think plot() returns a ggplot2 object, but when I try to add parameters to the plot() function (e.g., color = 'blue' or fill = 'blue'), nothing changes. Any help is appreciated! Reproducible example below.
library(sentimentr)
library(magrittr)
library(dplyr)
out <- presidential_debates_2012 %>%
get_sentences() %$%
sentiment_by(dialogue, list(person))
plot(out)
After starting your R session, type:
trace(sentimentr:::plot.sentiment_by, edit = T)
change everything what you want, e.g. change "red" to "blue" on line 21 to change the color of the points. Then repeat what you did in your example:
library(sentimentr)
library(magrittr)
library(dplyr)
out <- presidential_debates_2012 %>%
get_sentences() %$%
sentiment_by(dialogue, list(person))
plot(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
)

R Highcharter, hw_grid inside of hw_grid

I am using the highcharter package in R to create subplots, a function that highcharter calls hw_grid. It's fairly straightforward to create a couple plots stacked on top of one another using the following as a code example to get a 3 x 1 grid of charts.
library(dplyr)
library(highcharter)
MyData <- tibble(xvals=c(1,2,3),yvals=c(4,5,6))
h1 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h2 <-highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h3 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
subplot <- hw_grid(h1,h2,h3,ncol = 1)
What I'd like is for chart number 2 to be a subplot in and of itself, but the hw_grid function doesn't seem to like being nested. In other words, instead of having a 1/1/1 layout of charts vertically, I'd have a 1/2/1 layout of charts, where my second row contains two charts side by side. Here would be an example of how I would expect that to work.
library(dplyr)
library(highcharter)
MyData <- tibble(xvals=c(1,2,3),yvals=c(4,5,6))
h1 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h2 <-highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h3 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h4 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
hSubplot <- hw_grid(h2,h3,ncol=2)
subplot <- hw_grid(h1,hSubplot,h4,ncol = 1)
Thanks in advance for your assistance

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.

ggvis slider for bar plots

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

Resources