Search code examples
rr-markdownplotly

R Markdown and Plotly: fig.align not working with HTML output


fig.align is not working with R Markdown HTML outputs and plotly and I looking for some help!

Here is a MWE:

---
title: "Untitled"
output: html_document
editor_options: 
chunk_output_type: console
---

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


```{r}
plot_ly(
  x = c("giraffes", "orangutans"),
  y = c(20, 14),
  name = "SF Zoo",
  type = "bar")
```


```{r, fig.align = 'right'}
plot_ly(
  x = c("giraffes", "orangutans"),
  y = c(20, 14),
  name = "SF Zoo",
  type = "bar")
```

```{r, fig.align = 'center'}
plot_ly(
  x = c("giraffes", "orangutans"),
 y = c(20, 14),
 name = "SF Zoo",
type = "bar")
```

Solution

  • Unlike normal plots, plotly graphics have a different HTML setup. What you can do is use the shiny package and wrap another div container around each plot:

    library(shiny)
    div(plot_ly(
    x = c("giraffes", "orangutans"),
    y = c(20, 14),
    name = "SF Zoo",
    type = "bar"), align = "right")