Change Font Size on rCharts Sankey Diagram - r

I'm using the following code to create a Sankey Diagram using rCharts. I wish to increase the font size of the text printed on the Sankey Diagram. I can't find a manual to show me how to do this. Thoughts?
rm(list = ls())
require(reshape)
require(rCharts)
require(rjson)
target <- c('TMF', 'TMF', 'TMF','Evaporation','Mill Reclaim','Void Losses','Seepage')
source <- c('Precipitation & Run Off','Slurry','Other','TMF','TMF','TMF','TMF')
value <- c(638,1610,755,118,1430,466,2)
x <- data.frame(target,source,value)
sankeyPlot <- rCharts$new()
sankeyPlot$set(
data = x,
nodeWidth = 10,
nodePadding = 10,
layout = 32,
width = 1100,
height = 675,
units = "cubic metres",
title = "Sankey Diagram"
)
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot

Based on this answer you can add scripts to customize your plots. To change the text-size, you could add:
sankeyPlot$setTemplate(
afterScript = "
<script>
d3.selectAll('#{{ chartId }} svg text')
.style('font-size', '55')
</script>
")

I was able to change the font size after creating the sankey diagram by setting the fontSize option:
#create sankey diagram
p<-sankeyNetwork(...)
#the default font size
> p$x$options$fontSize
[1] 7
#set desired font size
p$x$options$fontSize<-10

Related

How to chage the background color of a plot in shiny?

I'm wondering if i can change the background-color of the plot or at least make it transparent allowing to inherit its parent background ,
I have tried that :
ui:
plotOutput("scatterChart",
width = "80%",
height = "294px")
server:
output$scatterChart <- renderPlot({
par(bg = "yellow")
plot(rules(), col = rainbow(25), cex = input$cex)
})
as shown here : https://stat.ethz.ch/pipermail/r-help/2003-May/033971.html
but nothing changed .
I tried that with css :
#scatterChart{
background-color:red}
i didn't get the expected result .
or :
.shiny-plot-output{
background-color:red
}
that change the entire div background and i even can't see the plot itself(i was exepecting that ).
Here is a picture :
demo
EDITED :
Based on the example that thothal gives me , i discover that the problem was on the data passed to the plot function (it just some association rules obtained using Apriori algorithm) :
rules <- reactive({
head(read.csv(input$file$datapath), input$visualization)
transactions = read.transactions(
file = file(input$file$datapath),
format = "basket",
sep = ","
)
minValue <- min(length(transactions),input$visualization)
rules <-
apriori(transactions[0:minValue],
parameter = list(
support = input$min_supp,
confidence = input$min_conf
))
return(rules)
})
Any suggestions or advice would be appreciated. Thanks.
Actually you can simply change the background color of you plot like this
library(shiny)
ui <- fluidPage(plotOutput("p"), actionButton("go", "Go"))
server <- function(input, output) {
output$p <- renderPlot({
input$go
par(bg = "navyblue")
x <- rnorm(100)
plot(x, 5 * x + rnorm(100, sd = .3), col = "white")
})
}
shinyApp(ui, server)
This produces the following plot on my machine:
As you tried the very same, I was wondering what happens if you try to create the plot outside shiny does it show (with the respective par call) a colorful background?
Maybe some other settings in you app may override this behaviour. Can you try to run my code and see what happens?
If you use another plotting library (ggplot for instance) you have to adapt and use
theme(plot.background = element_rect(...), # plotting canvas
panel.background = element_rect(...)) # panel
Update
It turns out that the culprit is arulesViz:::plot.rules, which is grid based and ignores settings set via par. To get a colored background we have to add a filled rect to the right viewport.
I forked the original repo and provided a quick fix of that:
devtools::install_github("thothal/arulesViz#add_bg_option_scatterplot")
data(Groceries)
rules <- apriori(Groceries, parameter=list(support=0.001, confidence=0.8))
## with my quick fiy you can now specify a 'bg' option to 'control'
plot(rules, control = list(bg = "steelblue")
We can use bg argument inside renderPlot:
output$plot1 <- renderPlot({
# your plot
}, bg = "red")

How to Save as png with ChartJSRadar in R?

I'm trying to save my plot with resolution of 300 for publication purposes. The usual methods to save plots with png device isn't working and saves a blank png. Is there something else I can try, or a different package that does something similar?
library(radarchart)
data<-data.frame(Field=c("Age","Sex","Submission"), y=sample(1:100,3), x=sample(1:100,3))
path<-"C:\\Desktop\\R\\"
png(file=paste0(path,"Radar",".png"), width=500, height=500, res=300)
plot<-chartJSRadar(scores=data, labelSize= 10, main="Completeness Radar", maxScale = 100)
print(plot)
dev.off()
I've also tried:
png(file=paste0(path,"Radar",".png"), width=500, height=500, res=300)
chartJSRadar(scores=data, labelSize= 10, main="Completeness Radar", maxScale = 100)
dev.off()
library(radarchart)
library(webshot)
library(htmlwidgets)
dat <- data.frame(
Field = c("Age","Sex","Submission"),
y = sample(1:100,3),
x = sample(1:100,3)
)
plt <- chartJSRadar(
scores = dat,
labelSize= 10,
main="Completeness Radar",
maxScale = 100
)
saveWidget(plt, "plt.html")
webshot("plt.html")
magick::image_read("webshot.png")
radar charts are very difficult for folks to grok
data and plot are suberbad variable names
whitespace is your bff
webshot can limit target area
various magick ƒ()s can crop target area
consider using http://www.ggplot2-exts.org/ggradar.html

using Highcharters in R with Shiny, control highstock chart height failed

I called highchart with height parameter to make a taller chart successfully in RStudio:
chart <- highchart(type = "stock", height=800, ...)
When in Shiny, the chart shown up but ignore the height parameter. Is there any way to fix it ?
If you are using shiny you can set the dimension in the highchartOutput() function:
# ui.R
...
highchartOutput("my_hc_id", width = 450, height = 300)
...
Other way using the hc_size function compared with the #pork-chop answer:
library(quantmod)
library(highcharter)
x <- getSymbols("GOOG", auto.assign = FALSE)
b <- hchart(x)
b <- hc_size(b, width = 450, height = 300)
b
Try this:
library(quantmod)
library(highcharter)
x <- getSymbols("GOOG", auto.assign = FALSE)
a <- hchart(x)
a$width <- 450
a$height <- 300
a

RStudio Not Rendering Sankey

Looking for some help identifying why this code would not render the Sankey in RStudio viewer or otherwise. I think it could be a problem with my R version (3.3.1)...the packages I'm using were built under 3.3.1. Unfortunately my organization takes awhile to update the enterpirse...
library(devtools)
library(rjson)
library(igraph)
library(rCharts)
x = read.csv('./R/dataforR.csv', header=TRUE)
colnames(x) <- c("UID","From", "To", "Avg.Hours")
sankeyPlot <- rCharts$new()
sankeyPlot$set(
data = x,
nodeWidth = 115,
nodePadding = 110,
layout = 132,
width = 1500,
height = 1300,
units = "hrs",
title = "New Sankey"
)
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot
The address in sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey') is incorrect.
As per this SO question, it should be:
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey')

Adding Color to Sankey Diagram in rCharts

I've created a sankey diagram in rCharts but have one question. How do I add color? I'd like to represent each node with a different color so it's easier to vizualize the paths, instead of just seeing the same grey lines connecting everything. Code and output below:
require(rCharts)
require(rjson)
x = read.csv('/Users/<username>/sankey.csv', header=FALSE)
colnames(x) <- c("source", "target", "value")
sankeyPlot <- rCharts$new()
sankeyPlot$set(
data = x,
nodeWidth = 15,
nodePadding = 10,
layout = 32,
width = 500,
height = 300,
units = "TWh",
title = "Sankey Diagram"
)
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot
Here is what my chart looks like
Thanks so much!
not sure what colors you want, but if you have installed the newer rCharts with devtools::install_github("ramnathv/rCharts"), here is how you might color based on the source value with a demo here.
require(rCharts)
require(rjson)
x = read.csv('/Users/<username>/sankey.csv', header=FALSE)
colnames(x) <- c("source", "target", "value")
sankeyPlot <- rCharts$new()
sankeyPlot$set(
data = x,
nodeWidth = 15,
nodePadding = 10,
layout = 32,
width = 500,
height = 300,
units = "TWh",
title = "Sankey Diagram"
)
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot$setTemplate(
afterScript = "
<script>
// to be specific in case you have more than one chart
d3.selectAll('#{{ chartId }} svg path.link')
.style('stroke', function(d){
//here we will use the source color
//if you want target then sub target for source
//or if you want something other than gray
//supply a constant
//or use a categorical scale or gradient
return d.source.color;
})
//note no changes were made to opacity
//to do uncomment below but will affect mouseover
//so will need to define mouseover and mouseout
//happy to show how to do this also
// .style('stroke-opacity', .7)
</script>
")
sankeyPlot
If you wanted to use a d3.scale.category??() to provide your color, I assume you would want to also similarly color the node rectangle. Here is one example of changing the color for both the node and the link.
sankeyPlot$setTemplate(
afterScript = "
<script>
var cscale = d3.scale.category20b();
// to be specific in case you have more than one chart
d3.selectAll('#{{ chartId }} svg path.link')
.style('stroke', function(d){
//here we will use the source color
//if you want target then sub target for source
//or if you want something other than gray
//supply a constant
//or use a categorical scale or gradient
//return d.source.color;
return cscale(d.source.name);
})
//note no changes were made to opacity
//to do uncomment below but will affect mouseover
//so will need to define mouseover and mouseout
//happy to show how to do this also
// .style('stroke-opacity', .7)
d3.selectAll('#{{ chartId }} svg .node rect')
.style('fill', function(d){
return cscale(d.name)
})
.style('stroke', 'none')
</script>
")
sankeyPlot

Resources