Canovice
Canovice

Reputation: 10173

Paper border on plotly R graph

Not sure if R's plot_ly function has this capability (i havent been able to find it), but figured I'd ask). Plot_ly does have a paper_bgcolor parameter that changes the color of the paper that the plot is on, as such:

mydf = data.frame(x = 1:5, y = 1:5)

plot_ly(mydf) %>%
  add_trace(x = ~x, y = ~x, type = 'scatter', mode = 'markers',
            marker = list(size = 24)) %>%
  layout(paper_bgcolor = 'red')  

I am not interested in changing the paper color, but rather the paper border. That is, I'd like a red box around the outer edge of the paper. Does anybody know a simple way to do this?

EDIT — I think I may have to use the grid package for this / it could be done with grid, but I'm curious first if it's possible without incorporating another package.

Upvotes: 2

Views: 2706

Answers (1)

Maximilian Peters
Maximilian Peters

Reputation: 31689

One possibility would be to modify the CSS file used by Plotly.

  • Find the location of your directory storing the plotly-min.js and the CSS file

    p <- plot_ly()
    p$dependencies[[4]]$src$file
    
  • Output:

    1 "/that/is/my/directory/Documents/R/win-library/3.4/plotly/htmlwidgets/lib/plotlyjs"

  • Go to this directory and copy plotly-htmlwidgets.css to a new file, e.g. plotly-htmlwidgets_red.css

  • Add the following lines to the new file

    div.svg-container {
      outline:5px solid red;
    }
    
  • Tell plotly to use your new CSS file

    p <- plot_ly()
    p$dependencies[[4]]$stylesheet <- "plotly-htmlwidgets_red.css"
    

Result

enter image description here

Upvotes: 2

Related Questions