简体   繁体   English

R Plotly:如何更改外框的颜色?

[英]R Plotly: How to change the color of the outer border?

I am creating a plot with .html format. 我正在创建一个.html格式的绘图。 The plot has a black background and I am wondering how to change the white border to black? 该情节具有黑色背景,我想知道如何将白色边框更改为黑色?

I notice that if I define the dimension of the plot (autoscale is set to FALSE), there are a lot of white space outside. 我注意到,如果定义图的尺寸(自动缩放设置为FALSE),则外面会有很多空白。

Thank you very much. 非常感谢你。

Sorry for not posting an example, this is my first time posting. 抱歉,没有发布示例,这是我第一次发布。

Here is one simple example: 这是一个简单的示例:

labels = paste((data/sum(data))*100,"%")

values = c(53, 43, 77, 33)

p1 = plot_ly(labels=labels,
             values=values,
             type="pie",
             hoverinfo = "label+percent",
             showlegend = FALSE,
             sort = FALSE
)

p1 = layout(p1,
            paper_bgcolor="rgb(31,31,31)",
            plot_bgcolor="rgb(31,31,31)",
            legend = list(font = list(color = "white"),
                          bgcolor = "transparent"),
            autosize = T,
            xaxis = list(
              color = "transparent"
            ),
            yaxis = list(
              color = "transparent"
            )
)

p1

Plotly Pie Chart 饼图 饼图

This is a screenshot of the plot. 这是剧情的屏幕截图。 As you can see there is a thin white border around the plot. 如您所见,情节周围有一个细的白色边框。

Sounds like you want a different default in the htmlwidget sizing policy. 听起来好像您想要htmlwidget大小调整策略中的其他默认值。 Try something like this: 尝试这样的事情:

p1$sizingPolicy$padding <- 0

I assume that this is a bug in plotly. 我认为这是一个错误。 When I inspect the created html it shows the following: 当我检查创建的html时,它显示以下内容:

<body style="margin: 5px; padding: 0px; overflow: hidden; width: 100%; height: 100%; background-color: white;">
<div id="htmlwidget_container" style="position: absolute; top: 5px; right: 5px; bottom: 5px; left: 5px;">
<div id="htmlwidget-3128" class="plotly html-widget html-widget-static-bound js-plotly-plot" style="width: 100%; height: 100%;">

<div class="plot-container plotly"><div class="svg-container" style="position: relative; width: 1270px; height: 403px;">

[...]

That means, the actual plotly container is embedded in a widget container with 5px. 这意味着,实际的plotly容器被嵌入到具有5px的小部件容器中。

Now if you want only to beautify your one html, change the beginning tags to the following: 现在,如果您只想美化自己的html,请将开始标记更改为以下内容:

<body style="margin: 0px; padding: 0px; overflow: hidden; width: 100%; height: 100%; background-color: white;">
<div id="htmlwidget_container" style="position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px;">

If you want everybody to profit, report this additionally as a bug over at plotlys project page. 如果您希望每个人都从中获利,请在plotlys项目页面上另外报告此错误。

In addition to @Nikolay `s answer 除了@Nikolay的答案

If your want do change some styles in plotly you can convert it to htmlwidget and then use like: 如果您确实想更改某些样式,则可以将其转换为htmlwidget,然后使用如下方式:

library(plotly)
values = c(53, 43, 77, 33)
labels = paste((values/sum(values))*100,"%")


p1 = plot_ly(labels=labels,
             values=values,
             type="pie",
             hoverinfo = "label+percent",
             showlegend = FALSE,
             sort = FALSE
)
style(p1,'#htmlwidget_container{
          position: absolute; 
      top: 0px; right: 0px; bottom: 0px; left: 0px;
      }')

p1 = layout(p1,
            paper_bgcolor="rgb(31,31,31)",
            plot_bgcolor="rgb(31,31,31)",
            legend = list(font = list(color = "white"),
                          bgcolor = "transparent"),
            autosize = T,
            xaxis = list(
              color = "transparent"
            ),
            yaxis = list(
              color = "transparent"
            )

)

library(htmltools)
library(htmlwidgets)

p2=as.widget(p1)
appendContent(p2,tags$head(tags$style('
          #htmlwidget_container{
            position: absolute  !important;
            top: 0px  !important; 
            right: 0px  !important;
            bottom: 0px  !important;
            left: 0px  !important;
                                  }')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM