Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
```
like image 839
John Stud Avatar asked Feb 04 '23 03:02

John Stud


1 Answers

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")
like image 142
Martin Schmelzer Avatar answered Feb 07 '23 01:02

Martin Schmelzer