仅在 RMarkdown 中将一个交互式绘图图表渲染为 HTML

蜜蜂

我正在使用 R markdown 绘制曲面

p1 <- plot_ly() %>% add_surface(z=z,x=wRange,y=yRange) %>% layout(showlegend=FALSE,scene=list(xaxis=list(title="wMult"),yaxis=list(title="yMult"),zaxis=list(title="MAE")))
p1

稍后我想通过这样做在这个表面上添加一个点:

p2 <- p1 %>% add_markers(z=MAE1,x=wMult1,y=yMult1) %>% layout(showlegend=FALSE)
p2

不久之后,我尝试通过在 p2 上添加另一个标记来绘制 p3。

p3 <- p2 %>% add_markers(z=MAE2,x=wMult2,y=yMult2) %>% layout(showlegend=FALSE)
p3

不幸的是,只有 p1 在 HTML 中呈现为交互式图表。p2 和 p3 显示为一个空白区域,其大小与图表的大小大致相同,但在查看器和浏览器中均不包含任何内容。如果我使用 web 检查器,我可以看到它正在尝试渲染一个 plotly 对象,但它看起来是空的。

如果我直接在 RStudio 中运行相同的代码,我可以查看添加了额外标记的图,但是当我编织降价时它们不会呈现。

这里发生了什么?

数据集可在此处获得:https : //archive.ics.uci.edu/ml/datasets/auto+mpg

到目前为止,这是完整的降价代码:

---
title: "Gradient Descent Demo"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
setwd("[your working directory]")
data = read.csv("mpg.csv")
require(plotly)
```

## Our Dataset

Let's take a look at some sample data. It shows attributes of several old cars and their fuel economy measured in miles per gallon. 

```{r c1}
head(data)
```

We'll try to predict a given car's mpg using only its weight and year.
```{r c2}
data <- data[,c("mpg","weight","year")]
pairs(data)
```

## Create a Hypothesis
Our hypothesis will be that we can get an approximation of the mpg by multipling the car's weight by some number "wMult" and adding that to the car's year multiplied by some other number "yMult". Let's just pick some numbers to start.
```{r c3, include=FALSE}
mod1 <- lm(mpg~weight+year,data=data)
bias1 <- mod1$coefficients[1]
```
```{r}
wMult1 <- -.02
yMult1 <- -2
```
We can turn this into a prediction.
(Ignore the bias - I cheated and did some behind-the-scenes pre-work.)
```{r c4}
data$mpgPred1 <- wMult1*data$weight + yMult1*data$year + bias1
head(data)
```
Ok so we have predictions. They're clearly pretty bad since they're negative, and most cars don't get negative miles per gallon. But can we measure how bad they are?

## Evaluate the Hypothesis
We need some measure of how good (or bad) our prediction is. We'll use the Mean Absolute Error ("MAE"). As the name suggests, this is calculated finding the average of the absolute difference between each predicted value and actual value.
```{r c5}
MAE1 <- mean(abs(data$mpgPred1-data$mpg))
MAE1
```
Ok so on average we're only off by about 250 mpg. Surely we can do better.

## Adjust the Hypothesis
What to use for our next hypothesis? Well we assign new wMult and yMult values and see how we do.
```{R c6}
wMult2 <- wMult1 + .03
yMult2 <- wMult2 - 1.2
data$mpgPred2 <- wMult2*data$weight + yMult2*data$year + bias1
head(data)
```
Our predictions look better (At least they're positive!), but they're still pretty far off. Let's see how much better or worse they are.

## Evaluate the Hypothesis - Round 2
```{R c7}
MAE2 <- mean(abs(data$mpgPred2-data$mpg))
MAE1
MAE2
```
Now we're only off by 50 on average. Still pretty terrible, but better than before.

## Adjust the Hypothesis - There has to be a better way.
Ok so instead of just continuing to make random guesses, let's develop a way to intelligently update our hypothesis.

Thankfully, since we're only using two variables for our analysis, we can pretty easily visualize the effect of every reasonable combination of wMult and yMult.
```{R c8, include=FALSE}
plotdata <- data.frame(wCoef=double(),yCoef=double(),MAE=double())
wRange <- seq(mod1$coefficients[2]-300*summary(mod1)$coefficients["weight","Std. Error"],mod1$coefficients[2]+300*summary(mod1)$coefficients["weight","Std. Error"],length.out=201) 
yRange <- seq(mod1$coefficients[3]-300*summary(mod1)$coefficients["year","Std. Error"],mod1$coefficients[3]+300*summary(mod1)$coefficients["year","Std. Error"],length.out=201)
for(i in wRange)
{for(j in yRange)
{
  preds <- (i*data$weight) + (j*data$year) + bias1
  resid <- preds-data$mpg
  MAE = mean(abs(resid))
  newRec <- data.frame(wCoef=i,yCoef=j,MAE=MAE)
  plotdata <- rbind(plotdata,newRec)
}
}
z <- matrix(plotdata$MAE,nrow=201,ncol=201)
```
```{R c9}
p1 <- plot_ly() %>% add_surface(z=z,x=wRange,y=yRange) %>% layout(showlegend=FALSE,scene=list(xaxis=list(title="wMult"),yaxis=list(title="yMult"),zaxis=list(title="MAE")))
p1
```
Great - we can visibly explore this graph and see what some good weights might be. The best one is the one that minimizes the MAE. That's the center spot at the middle of the valley, where the crease seems to dip slightly.

Let's add our first hypothesis to this chart to see where it falls.
```{R c10,warning=F}
p2 <- p1 %>% add_markers(z=MAE1,x=wMult1,y=yMult1) %>% layout(showlegend=FALSE)
p2
```
And let's add our second one
```{R c11}
p3 <- p2 %>% add_markers(z=MAE2,x=wMult2,y=yMult2) %>% layout(showlegend=FALSE)
p3
```
Ok so it turns out our second guess actually overshot. This means that if we kept updating our hypothesis in the same manner, we'd actually get worse with each new step.

## Letting the machine do it
As I mentioned before, this approach works because we only have 2 variables we're working with. But if we had more, we'd be dealing with spaces greater than 3 dimensions. This gets hard to visualize.

Thankfully there's a way for the machine to navigate those higher dimensional spaces. We'll continue to use this two dimensional approach for now to help illustrate the approach.
瑞安·莫顿

在 HTML 领域,像这样id属性很重要。发生的事情是情节的divid 继承自先前的情节。这在 HTML 中是不允许的。因此,您每次都需要重新创建绘图,以免它们继承绘图 ID。我找不到plotly重置 id 以防止出现此问题函数,所以我的答案是遵循严格的“不继承以前的情节”政策:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
```

# First plot

```{r pressure, echo=FALSE}
p1 <- plot_ly(source = "plot1") %>%
  add_markers(x = pressure$temperature, y = pressure$pressure)
p1
```

# Second plot

```{r pressure2, echo= FALSE}
p2 <- plot_ly(source = "plot2") %>%
  add_markers(x = pressure$temperature, y = pressure$pressure) %>%
  add_markers(x = pressure$temperature, y = pressure$pressure+10)
p2
```

旁注:如果您处于 Shiny 环境中,则每个图都将包含在需要唯一命名的渲染/输出组合中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用交互式绘图仅在Altair中缩放一个轴

来自分类Dev

如何仅在一个Window / Tab中打开HTML

来自分类Dev

如何从RMarkdown渲染HTML而无需在输出中使用javascript

来自分类Dev

将RMarkdown编织为HTML时如何设置主题?

来自分类Dev

如何在交互式RMarkdown中控制绘图高度/大小(使用Shiny)

来自分类Dev

如何最好地基于一个数据集从RMarkdown生成多个HTML文件?

来自分类Dev

仅在Dom中将HTML解释作为字符串注入

来自分类Dev

如何将离线 Plotly 图表导出为交互式 HTML

来自分类Dev

RMarkdown html_output错误

来自分类Dev

$()。html()仅在第一次使用

来自分类Dev

HTML表格仅在一页上变形

来自分类Dev

将RMarkdown编织为HTML时出错(HTML中未显示任何图形)

来自分类Dev

rmarkdown中一个函数的多个绘图输出

来自分类Dev

使用汤仅在html页面中查找一个元素

来自分类Dev

使用汤仅在html页面中查找一个元素

来自分类Dev

Typo3:仅在最后一个HTML路径段上使TemplaVoila映射

来自分类Dev

php - html 表单 $_POST 仅在表单的一个元素上未定义索引?

来自分类Dev

如何使用rmarkdown使绘图表调整为页面宽度?

来自分类Dev

rmarkdown:悬挂式引文

来自分类Dev

使用kable垂直对齐列名称,并使用rmarkdown渲染到html中

来自分类Dev

HTML边框仅在表格外

来自分类Dev

在rmarkdown中为r块中生成的html添加toc条目

来自分类Dev

在rmarkdown html中调整传单地图的大小

来自分类Dev

跟随HTML编织-RMarkdown包括空白块

来自分类Dev

Rmarkdown HTML矩阵输出宽度太窄

来自分类Dev

在RMarkdown HTML输出中使用内部链接

来自分类Dev

Rmarkdown生成的HTML表出现颜色问题

来自分类Dev

Rmarkdown中的HTML标签到Word文档

来自分类Dev

Rmarkdown HTML矩阵输出宽度太窄

Related 相关文章

  1. 1

    使用交互式绘图仅在Altair中缩放一个轴

  2. 2

    如何仅在一个Window / Tab中打开HTML

  3. 3

    如何从RMarkdown渲染HTML而无需在输出中使用javascript

  4. 4

    将RMarkdown编织为HTML时如何设置主题?

  5. 5

    如何在交互式RMarkdown中控制绘图高度/大小(使用Shiny)

  6. 6

    如何最好地基于一个数据集从RMarkdown生成多个HTML文件?

  7. 7

    仅在Dom中将HTML解释作为字符串注入

  8. 8

    如何将离线 Plotly 图表导出为交互式 HTML

  9. 9

    RMarkdown html_output错误

  10. 10

    $()。html()仅在第一次使用

  11. 11

    HTML表格仅在一页上变形

  12. 12

    将RMarkdown编织为HTML时出错(HTML中未显示任何图形)

  13. 13

    rmarkdown中一个函数的多个绘图输出

  14. 14

    使用汤仅在html页面中查找一个元素

  15. 15

    使用汤仅在html页面中查找一个元素

  16. 16

    Typo3:仅在最后一个HTML路径段上使TemplaVoila映射

  17. 17

    php - html 表单 $_POST 仅在表单的一个元素上未定义索引?

  18. 18

    如何使用rmarkdown使绘图表调整为页面宽度?

  19. 19

    rmarkdown:悬挂式引文

  20. 20

    使用kable垂直对齐列名称,并使用rmarkdown渲染到html中

  21. 21

    HTML边框仅在表格外

  22. 22

    在rmarkdown中为r块中生成的html添加toc条目

  23. 23

    在rmarkdown html中调整传单地图的大小

  24. 24

    跟随HTML编织-RMarkdown包括空白块

  25. 25

    Rmarkdown HTML矩阵输出宽度太窄

  26. 26

    在RMarkdown HTML输出中使用内部链接

  27. 27

    Rmarkdown生成的HTML表出现颜色问题

  28. 28

    Rmarkdown中的HTML标签到Word文档

  29. 29

    Rmarkdown HTML矩阵输出宽度太窄

热门标签

归档