Forum Discussion
JVidal
Helper I
7 years ago[SOLVED] R plotly custom visualization margin padding
Good morning all, I am creating a custom visualization with R with the plotly package. The issue I am having is with the margin padding the custom visualization is adding to the output. I ha...
- 7 years ago
Good morning,
I have finally managed to get rid of the extra padding introduced in the visualization. I appears the bug is with htmlWidgets, that introduces padding into the html file.
A simple R function can get rid of the padding, such as the one below:
ReadFullFileReplaceString <- function(fnameIn, fnameOut, sourceString,targetString) { if(!file.exists(fnameIn)) return(NULL) tx <- readLines(fnameIn,encoding = "UTF-8") tx2 <- gsub(pattern = sourceString, replace = targetString, x = tx) writeLines(tx2, con = fnameOut) }The calling the function like so:
ReadFullFileReplaceString('out.html', 'out.html', ',"padding":[0-9]*,', ',"padding":0,')Credit goes to MS, as the solution was found in one of their R visualizations, PowerBI-visuals-forcasting-exp
in their script.r file: https://github.com/microsoft/PowerBI-visuals-forcasting-exp/blob/master/script.r
Anonymous
7 years agoNot applicable
JVidal ,
I'm new to R script, where would I add your code in my script to remove the padding?
Below is my R script example;
source('./r_files/flatten_HTML.r')
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
df <- data.frame(RunID = Values$RunID, CLWT = Values$xCLw, PCLF = Values$xPCLFw)
g = ggplot(df, aes(x=PCLF, y=CLWT, color= as.factor(RunID)))+
geom_point(size=2)+
labs(title="Pan Polar", x = "% CLF",y = "CL")+
theme(text=element_text(size=8),
plot.margin = unit(c(0.1,1,0.1,0.1),"cm"))
p = ggplotly(g) %>% config(displaylogo = FALSE,
modeBarButtonsToRemove = list
('toImage',
'zoomIn2d',
'zoomOut2d',
'lasso2d',
'hoverCompareCartesian',
'select2d'));
internalSaveWidget(p, 'out.html');