在slidify中调整绘图图表的大小 [英] Adjust the size of plotly charts in slidify

查看:138
本文介绍了在slidify中调整绘图图表的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在R中创建了一个绘图图表,然后通过htmlwidget保存该绘图,以便可以在slidify中调用该绘图.我在layout参数和index.Rmd中都使用了widthheight,以使图适合滑动幻灯片,但是当将一些文本添加在一起时,最终结果总是会切掉图表的底部.

如何在slidify中真正调整绘图图表的大小?

R脚本中的代码

## first slide
date <- seq(from = as.POSIXct("2015/4/1"),
            to = as.POSIXct("2015/10/1"),
            by = "month")

as.Date(date, "%Y/%m/%d")

set.seed(2016)
tot.prem <- runif(n = 7, min = 0, max = 1200)
pol.frce <- runif(n = 7, min = 0, max = 6000)
tot.data <- cbind(date, tot.prem, pol.frce)
tot.data <- data.frame(Date = as.POSIXct(date, format = "%Y/%m/%d"), 
                  WP = tot.prem, 
                  PIF = pol.frce)

m = list(
  l = 50,
  r = 50,
  b = 100,
  t = 100,
  pad = 4
)

p1 <- tot.data %>% 
  subplot(plot_ly(x = date, y = tot.prem, type = "bar"),
          plot_ly(x = date, y = pol.frce, type = "bar"), margin = 0.05) %>% 
  layout(showlegend = FALSE, 
         title = "Written Premium and PIF", 
         yaxis = list(title = "WP (in Millions)", showgrid = FALSE),
         xaxis = list(title = "Date"),
         yaxis2 = list(title = "Policy in Force", showgrid = FALSE),
         xaxis2 = list(title = "Date"),
         autosize = F, width = 600, height = 400, margin = m
         )
p1
# saveWidget(dplot, 'plotlyex1.html')
htmlwidgets::saveWidget(as.widget(p1), "wp.html")

index.Rmd的摘录

## Program Summary

- Point 1: blah blah blah
- Point 2: blah blah blah
- Point 3: blah blah blah

```{r wp, echo=FALSE, warning=FALSE, cache=FALSE, results='asis'}
cat('<iframe src="./assets/widgets/wp.html" width=100% height=10% allowtransparency="true"> </iframe>')
```

---

幻灯片

解决方案

我找到了解决文本问题的方法,但没有找到您的尺寸查询.如果在图表下方放置一些文本,则x轴文本将正确显示.如果您不想输入文字,也可以使用空白字符,例如:

```{r wp, echo=FALSE, warning=FALSE, cache=FALSE, results='asis'}
cat('<iframe src="./assets/widgets/wp.html" width=100% height=10% allowtransparency="true"> </iframe>')
```
&#032;

您的代码帮助我在Slidify中首次显示了Plotly,因此谢谢您.

I've created a plotly chart in R and then save the plot via htmlwidget so the plot can called in slidify. I played with width and height, in both layout argument and index.Rmd to fit the plot on a slidify slide but the final result always cut the bottom portion of the chart when some texts are added together.

How can I truly adjust the size of the plotly chart in slidify?

The code in R script

## first slide
date <- seq(from = as.POSIXct("2015/4/1"),
            to = as.POSIXct("2015/10/1"),
            by = "month")

as.Date(date, "%Y/%m/%d")

set.seed(2016)
tot.prem <- runif(n = 7, min = 0, max = 1200)
pol.frce <- runif(n = 7, min = 0, max = 6000)
tot.data <- cbind(date, tot.prem, pol.frce)
tot.data <- data.frame(Date = as.POSIXct(date, format = "%Y/%m/%d"), 
                  WP = tot.prem, 
                  PIF = pol.frce)

m = list(
  l = 50,
  r = 50,
  b = 100,
  t = 100,
  pad = 4
)

p1 <- tot.data %>% 
  subplot(plot_ly(x = date, y = tot.prem, type = "bar"),
          plot_ly(x = date, y = pol.frce, type = "bar"), margin = 0.05) %>% 
  layout(showlegend = FALSE, 
         title = "Written Premium and PIF", 
         yaxis = list(title = "WP (in Millions)", showgrid = FALSE),
         xaxis = list(title = "Date"),
         yaxis2 = list(title = "Policy in Force", showgrid = FALSE),
         xaxis2 = list(title = "Date"),
         autosize = F, width = 600, height = 400, margin = m
         )
p1
# saveWidget(dplot, 'plotlyex1.html')
htmlwidgets::saveWidget(as.widget(p1), "wp.html")

Excerpt of index.Rmd

## Program Summary

- Point 1: blah blah blah
- Point 2: blah blah blah
- Point 3: blah blah blah

```{r wp, echo=FALSE, warning=FALSE, cache=FALSE, results='asis'}
cat('<iframe src="./assets/widgets/wp.html" width=100% height=10% allowtransparency="true"> </iframe>')
```

---

Slide

解决方案

I found a solution to the text problem but not your size inquiry. If you place some text below the graph then x axis text will display correctly. If you don't want text, a blank character also works, as in:

```{r wp, echo=FALSE, warning=FALSE, cache=FALSE, results='asis'}
cat('<iframe src="./assets/widgets/wp.html" width=100% height=10% allowtransparency="true"> </iframe>')
```
&#032;

You code helped me get my first Plotly display in Slidify, so thank you for that.

这篇关于在slidify中调整绘图图表的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆