绘图R图上的纸边框 [英] Paper border on plotly R graph
问题描述
不确定R的plot_ly函数是否具有此功能(我还没有找到它),但是想通了. Plot_ly确实有一个paper_bgcolor参数,该参数可以更改绘图所在纸张的颜色,例如:
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?
编辑-我想我可能必须为此使用grid包,也可以使用grid来完成,但是我首先好奇是否有可能不合并其他包.
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.
推荐答案
一种可能性是修改Plotly使用的CSS文件.
One possibility would be to modify the CSS file used by Plotly.
-
找到存储plotly-min.js和CSS文件的目录的位置
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"
1 "/that/is/my/directory/Documents/R/win-library/3.4/plotly/htmlwidgets/lib/plotlyjs"
转到此目录并将plotly-htmlwidgets.css
复制到新文件,例如plotly-htmlwidgets_red.css
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;
}
请告诉我们使用新的CSS文件
Tell plotly to use your new CSS file
p <- plot_ly()
p$dependencies[[4]]$stylesheet <- "plotly-htmlwidgets_red.css"
结果
这篇关于绘图R图上的纸边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!