Can I include tooltips in the d3pie rcharts implementation? - r

I am creating a d3pie graph with rcharts following this article.
I am able to create the pie chart and now I would like to add a tooltip similar to this one.
My code so far is:
require(magrittr)
require(dplyr)
require(rCharts)
df<-data.frame(label=c("group1","group2","group3"),value=c(1,5,4),
stringsAsFactors = FALSE)
pie1<-rCharts$new()
pie1$setLib("http://timelyportfolio.github.io/rChartsExtra/d3pie")
pie1$params$chartspec <- list(header = list(title = list(text = "Title"),
subtitle = list(text = "Subtitle")),
data = list(content = df),
labels = list(lines = list(style = "straight")),
tooltips=list(enabled=TRUE,type= "placeholder",
string="{label}"))
pie1
I am not sure if including the tooltips is possible with rcharts.
If it is possible my guess is that the problem comes from the enabled=TRUE. I also tried: enabled="[true]", enabled="{true}" and enabled=T. None of them seem to work.
I am starting to learn d3.js, however, I want to embed the chart in rmarkdown so I would prefer a solution based on R.

Related

Unselected entries displayed on axis - Crosstalk+Plotly bar-chart

EDIT
This seems to be an issue already known to the plotly community
github plotly issue #689
and there is an analogous question here on SO.
Unfortunately, it seems no solution is available yet. Any advice would be greatly appreciated.
I am trying to use Crosstalk and Plotly to create a dashboard and I have come across an unexpected behaviour.
When selecting through the Crosstalk filter, the Plotly bargraph leaves "gaps" for the unselected entries.
As a reproducible example, let's say I want to compare cities populations, what I am getting is this (code at the bottom):
It might very well be that I am missing something, is there a way to get rid of the gap? any advice on viable ways to do a similar comparison avoiding the issue?
Thanks in advance.
Code:
---
title: "Crosstalk+Plotly bargraph selection"
---
```{r setup, include=FALSE}
options(stringsAsFactors = FALSE)
library(crosstalk)
library(dplyr)
library(plotly)
#data on cities' population
city_pop <- data.frame("City" = c("Florence", "Milan", "Venice"),
"Population" = c(382258, 1352000, 261905))
#setting up Crosstalk shared data
sd <- SharedData$new(city_pop, key = city_pop$city)
#filter for the cities
filt <- filter_select(
id = "select_name",
label = "Selected City",
sharedData = sd,
group = ~City)
#barplot of cities' population
bars_pop <- plot_ly(sd, x = ~City, y = ~Population) %>%
add_bars(width=0.2,
x = ~City,
y = ~Population,
color = I("#89CFF0"),
name = "",
opacity=.9,
hoverinfo = 'y',
hovertemplate = paste('%{x} <br> number of Residents: %{y}<extra></extra>')
)
```
```{r, echo=FALSE}
filt
bars_pop
```
This worked for me - on the axis where it's happening, set categoryorder = "trace".
e.g.,
plot_ly(...) %>% layout(yaxis = list(categoryorder = "trace"))

officer chart removing border around chart

Does anyone know how I can remove the border around an ms chart object when I insert it in a word document using office R?
Here is the code to create the chart:
library(mscharts);library(officer)
test_data <- data.frame("Var1" = c(2,4,6,8), "Var2" = c(2,4,6,8), "Group" = c("A","B","A","B"))
my_barchart <- ms_barchart(data = test_data ,
x = "Var1", y = "Var2", group = "Group")
Then I insert it to the office r document with
body_add_chart(chart = my_barchart, style = "centered")
The chart looks fine, but appears with a border around it which I want to remove. I think you should be able to do this with the style argument but I can't quite figure out how.

R crashes when trying to add a plot on a powerpoint template

I have developed a dashboard using R and the library shiny, where some different charts are displayed. These ones aren't static charts but they are generated dynamically according to some filters you can set through some bottons and so on.
The next step, what I am working on right now it would be to create a powerpoint report including those charts I have generated before. I got this piece of code so far:
report <- pptx(template = 'report_template.pptx')
report <- addSlide(report,"report_layout")
slide.layouts(report)
slide.layouts(report, "report_layout")
report <- addPlot(report, function() print(p))
writeDoc(report, "example_report.pptx")
Being p a given chart created by plotly this way:
p <- plot_ly(agregado_cedex(), labels = ~Escalado, values = ~Total,type = "pie",
text = ~Total, width = 500, height = 250, hoverinfo = "none") %>% config(displayModeBar = FALSE) %>%
layout(title = "Desglose incidencias", showlegend = TRUE,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
There is no problem with this chart as it is correctly displayed on the shiny app (dashboard).
When executing the code above I am getting the next error on R Studio:
Warning: Error in .jcall: javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.]
I found something a similar post but it haven't been solved so far Add plots in PPTx without crashing R using addplot()
Does anyone know what is wrong? Thanks in advance!
ReporteRs does not support htmlwidgets. addPlot work with base plot commands, ggplot2, grid and lattice.

How to set dataLabel Format and axis label angle in R highcharter package

I am experimenting with R highcharter package to create a bar chart function. the code is as below. I request help in
1-How to change the format of the dataLabels to percentage ?
2-How to set X-axis label display angle. I want to set it to 45 degrees
hcbar_categorycount_vertical <- function(data=x,var=y){
df <- data.frame(prop.table(table(data[var])))
names(df) <- c(var,'Proportion')
df$Proportion <- round(df$Proportion*100,2)
df <- df%>% arrange(-Proportion)
df[,1] <- as.character(df[,1])
df[,1] <- factor(df[,1], levels = df[,1])
df$Cumulative <- round(cumsum(df$Proportion),2)
highchart(debug = TRUE) %>%
hc_xAxis(categories=df[[1]]) %>%
hc_yAxis(labels = list(format = "{value}%"), max = 100) %>%
hc_add_series(name=var,data=df$Proportion,type = "column",dataLabels = list(enabled = TRUE, format='{point.label}%'))
}
I am not sure what should be the syntax of "format" within dataLabel property list.The above code does not seem to work. I already referred to the highcharter vignette and this site : http://jkunst.com/highcharter/highcharts-api.html#hc_xaxis-and-hc_yaxis
But could not find an answer. Thanks for the help in advance.
#jeganathan-velu,
1) Try changing the '{point.label}%'by '{point.y}%'
2) See the highcharts example. You need to add to the hc_xAxis the argument labels = list(rotation = 90)
highcharter package is just the wrapper of highcharts so you can check all the examples and the well documented API from highcharts. Replicating highcharts demos
Found the answer after trial and error and some further research in http://api.highcharts.com/highcharts#xAxis.labels.rotation
Posting the updated code component for the benefit of others.
hc_xAxis(categories=df[[1]],labels = list(rotation=-45)) %>%
hc_yAxis(labels = list(format = "{value}%"), max = 100) %>%
hc_add_series(name=var,data=df$Proportion,type = "column",dataLabels = list(enabled = TRUE, format='{point.y}%'))

Shiny with plotly

I'm creating a notebook with shiny and i have found the following issue when using the add-on plotly:
Data_xts=Data_xts() #this return a reactive data.frame. fm_timeseries is already formated as POSIXct and has the following format : YYYY-MM-DD hh:mm:ss
plot_ly(x=Data_xts$fm_timeseries, y = Data_xts$Open,name='Stock Prices',showlegend = FALSE) %>%
add_trace(x=Data_xts$fm_timeseries, y = Data_xts$UKX_open, yaxis='y2') %>%
add_trace(x=Data_xts$fm_timeseries, y = Data_xts$TotalNews, yaxis='y3') %>%
layout(yaxis=list(title='Stock Price'),
yaxis2=list(title='Market Index', overlaying = "y",side='right',position=.97,tickfont = list(size = 8)),
yaxis3=list(title='Total News', overlaying = "y",side='right',anchor='free', tickfont = list(size = 8),position=1),
xaxis=list(title='Time Series') , showlegend = TRUE)
Here is a couple of issues I'm founding:
Time stamp don't show as timestamp (I even tried the simple example from plotly and R and verified that it doesn't work on Shiny). In fact timestamp is categorize as string and the xaxis tittle can't be seen as a result of this.
Challenges getting the yaxis2 and 3 in the same axes without overlapping with the chart itself.
To solve this wonder if there is a format that we can pass into the xaxis (tried type='date' but didn't work) and if there is any type of auto-arrange on the yaxis on the right (2 and 3).
Try using "factor".
So in your example:
plot_ly(x= ~factor(Data_xts$fm_timeseries), y = ~Data_xts$Open, name='Stock Prices',showlegend = FALSE)

Resources