如何删除RStudio查看器窗格中的空白/空白边距以用于绘图

时间:2017-12-11 10:33:51

标签: r rstudio plotly viewer

当我在情节中在RStudio查看器窗格中创建绘图时,我在绘图周围留下了一个大的白边。这通常很好,但在使用非标准配色方案时则不行。此外,当导出图像或在RMarkdown等中生成图像时,这个白色边缘似乎也存在。

有没有人知道如何删除它?

以下是一些示例代码:

library(plotly)

month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
           'August', 'September', 'October', 'November', 'December')
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)
data <- data.frame(month, high_2014, low_2014)
data$average_2014 <- rowMeans(data[,c("high_2014", "low_2014")])

#The default order will be alphabetized unless specified as below:
data$month <- factor(data$month, levels = data[["month"]])

plot_ly(
  data
  ,x = ~month
  ,y = ~high_2014
  ,type = 'scatter'
  ,mode = 'lines'
  ,line = list(color = 'rgba(0,100,80,1)')
  ,showlegend = FALSE
  ,name = 'High 2014'
)%>%
  add_trace(
    y = ~low_2014
    ,type = 'scatter'
    ,mode = 'lines'
    ,fill = 'tonexty'
    ,fillcolor='rgba(0,100,80,0.2)'
    ,line = list(color = 'rgba(0,100,80,1)')
    ,showlegend = FALSE
    ,name = 'Low 2014'
  )%>%
  layout(
    title = "High and Low Temperatures in New York"
    ,paper_bgcolor='navy'
    ,plot_bgcolor='rgb(229,229,229)'
    ,bgcolor = "black"
  )

Image of Problem

谢谢, 汤姆

2 个答案:

答案 0 :(得分:2)

您可以尝试类似于此处的方法:Paper border on plotly R graph

  • 查找存储plotly-min.js和CSS文件

    的目录的位置
    p <- plot_ly()
    p$dependencies[[4]]$src$file
    

    输出:

      

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

  • plotly-htmlwidget.css复制到另一个文件,例如plotly-htmlwidget_NoBorder.css

  • 将以下行添加到新文件

    body{
      padding: 0px !important;
    }
    
    #htmlwidget_container{
      top: 0px !important;
      bottom: 0px !important;
      right: 0px !important;
      left: 0px !important;
    }
    
  • 将您的情节图放在变量p

    p <- plot_ly()
    
  • 覆盖此图表的CSS

    p$dependencies[[4]]$stylesheet = "plotly-htmlwidgets_NoBorder.css"
    

enter image description here

答案 1 :(得分:0)

填充来自import {VehicleFactory} from '../VehicleFactory'中的默认选项。可以通过更改绘图小部件对象的大小调整策略来覆盖它。有关所有选项的文档,请参见htmlwidgets。这将消除所有查看器类型的填充:

?htmlwidgets::sizingPolicy