I'm new to latex and rmarkdown. I am trying to center a plotly chart and I have already tried fig.align = "center" and nothing happens, if to this command I add fig.show = 'hold', the plot is centered but the \begin{center} code is added after the plot image. I tried to add latex code directly before the chunk (\ begin {center}) and \ end {center} after, but it still appears in the pdf output visible latex code. Please I don't no what happen with this.enter image description here

This is my code

a <- list(
  autotick = FALSE,
  ticks = "outside",
  tick0 = 0,
  dtick = 1,
  ticklen = 0,
  tickwidth = 1,
  tickcolor = toRGB("blue")
avlbty<-plot_ly(df_merged, x = ~daytime, y = ~Availability, color = ~Series,type = 'scatter', mode = 'lines', fill = 'tozeroy', width =700, height = 350)
avlbty<-avlbty %>% 
  layout(title = "Availability by Service",yaxis = a,
         xaxis=list(title='Date'),font=f, margin = list(l=50, r=50, b=0, t=30, pad=0))
  • It is impossible to replicate this as we don't have access to df_merged or other items. Please produce a MWE.
    – oliversm
    May 6, 2020 at 10:38
  • my full code is here stackoverflow.com/questions/61618432/… @oliversm
    – Liliana
    May 6, 2020 at 11:07
  • Please help me to solve this problem, with fig.align='center' nothing happen
    – Liliana
    May 6, 2020 at 11:08

1 Answer 1


Option 1: Export figure and then include it again

In a similar manner to this answer to Plotly as png in knitr/rmarkdown the current hack is to export the figure and then load in the produced file. As a small example (which you could adapt) there is

title: "Diamond prices"
author: "Oliver"
date: "6^th^ May 2020"
    fig_caption: yes
   - \usepackage{cleveref}
fontsize: 12pt
always_allow_html: yes

# Making a plot

```{r, echo=FALSE, include=FALSE}
options(shiny.testmode = TRUE)

```{r , echo=FALSE, include=FALSE}
fig <- plot_ly(diamonds, x = ~carat, y = ~price, type="scatter", mode="markers", color = ~clarity, width =200, height = 150)
orca(fig, file="diamonds.png")

![\label{fig:fig_prices_of_diamonds}The price of diamonds depnds on their size.](diamonds.png)

We can see in \Cref{fig:fig_prices_of_diamonds} how expensive diamonds can be. 

which produces

enter image description here

Option 2: Use webshot

Based on comments in this answer to Change output width of plotly chart size in R Markdown PDF output an alternative is to capture the image automatically using wesbot. Simply run the following once in the R console to install webshot (and PhantomJS which webshot in turn uses):


One you have this you can adapt the above example to use instead:

```{r fig:fig_prices_of_diamonds, echo=FALSE, include=TRUE, fig.cap="\\label{fig:fig_prices_of_diamonds}The prices of diamonds increases perhaps?"}
plot_ly(diamonds, x = ~carat, y = ~price, type="scatter", mode="markers", color = ~clarity, width =400, height = 250)

We can see in \Cref{fig:fig_prices_of_diamonds} how expensive diamonds can be. 

which is much cleaner code.

  • But in your example, the chart occupies the entire page horizontally, so it is not centered. In my case, I want a plotly chart with smaller dimensions and horizontally align centered.
    – Liliana
    May 6, 2020 at 12:43
  • It only seems to fill the whole page and be justified as the figure is big. If I make the figure smaller it centers. I will update the answer shortly to show this.
    – oliversm
    May 6, 2020 at 17:24
  • ok thanks, I will try
    – Liliana
    May 7, 2020 at 9:18
  • @Liliana, I have added an alternative using Webshot where some parameters such as width and height can also be adjusted.
    – oliversm
    May 14, 2020 at 11:17
  • thanks again for helping me
    – Liliana
    May 14, 2020 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.