Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display python plotly graph in RMarkdown html document

Why plotly package of python can not display figure in RMarkdown but matplotlib can? For example:

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

```{r}
library(plotly)
subplot(
     plot_ly(mpg, x = ~cty, y = ~hwy, name = 'default'),
     plot_ly(mpg, x = ~cty, y = ~hwy) %>%
         add_markers(alpha = 0.2, name = 'alpha'),
     plot_ly(mpg, x = ~cty, y = ~hwy) %>%
         add_markers(symbols = I(1), name = 'hollow')
 )
```

```{python}
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

plotly.tools.set_credentials_file(username='xxx', api_key='xxx')

N = 500
trace0 = go.Scatter(x = np.random.randn(N), y = np.random.randn(N) + 2, name = "Above", mode = "markers",
marker = dict(size = 10, color = "rgba(152, 0, 0, .8)", line = dict(width = 2, color = "rgb(0,0,0)")))

trace1 = go.Scatter(x = np.random.randn(N), y = np.random.randn(N) - 2, name = "below", mode = "markers",
marker = dict(size = 10, color = "rgba(255, 182, 193, .9)", line = dict(width = 2, color = "rgb(0,0,0)")))

data = [trace0, trace1]
layout = dict(title = "Styled Scatter", yaxis = dict(zeroline = False), xaxis = dict(zeroline=False))
fig = dict(data = data, layout = layout)
py.iplot(fig, filename = "styled-scatter")
```

The R code can work well, but the python code can not dispay the figure, what is wrong with the code?

like image 349
littlely Avatar asked May 05 '18 15:05

littlely


People also ask

How do you display plotly in HTML?

To share a plot from the Chart Studio Workspace, click 'Share' button on the left-hand side after saving the plot. The Share modal will pop-up and display a link under the 'Embed' tab. You can then copy and paste this link to your website. You have the option of embedding your plot as an HTML snippet or iframe.

Can you use plotly in RMarkdown?

If you are creating R charts in an RMarkdown environment with HTML output (such as RStudio), simply printing a graph you created using the plotly R package in a code chunk will result in an interactive HTML graph in the viewer.

How do I show plot in RMarkdown?

In RStudio, when you open a new RMarkdown file, in the editor pane, there is a cogwheel button / menu where you can choose "Chunk Output Inline". That should put the plots into the document.

How do you display plotly plots?

In general, there are five different approaches you can take in order to display plotly figures: Using the renderers framework in the context of a script or notebook (the main topic of this page) Using Dash in a web app context. Using a FigureWidget rather than a Figure in an ipywidgets context.


1 Answers

Here is what I did:

  1. used plotly offline:
    • replace import plotly.plotly as py by import plotly.offline as py
    • no need to set username and api key in offline mode.
  2. used py.plot(fig, filename = "styled-scatter.html", auto_open=False):
    • py.iplot() is for Jupyter notebooks (it embeds the plot directly into the Notebook)
    • auto_open = False argument is to avoid that the plot pops up.
  3. embedded the html plot into the Rmarkdown by using the following:

    ```{r, echo=FALSE}
    htmltools::includeHTML("styled-scatter.html")
    ```
    

    and here is the result: enter image description here

like image 153
byouness Avatar answered Sep 18 '22 01:09

byouness