Search code examples
rr-highcharter

Adjusting highcharter plot dimensions in Rmarkdown


I am using highcharter plots in Rmarkdown and I can't seem to adjust the height or the width (in RStudio). Any suggestions? Details below:

If I use the basic default code;

```{r}
library(highcharter)

cars %>%
  hchart(.,"scatter",hcaes(x=speed,y=dist))
```

I get this:

enter image description here

That doesn't change if I edit chunk options fig.height=4,fig.width=8 or if I change dimensions in the plot hc_chart(height=800,width=600).

If I save the plot and print using a taglist, then it starts to become usable.

```{r}
h <- cars %>%
  hchart(.,"scatter",hcaes(x=speed,y=dist))

htmltools::tagList(list(h))
```

enter image description here

But, I still can't adjust dimensions using chunk options or using internal plot dimensions.

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
highcharter_0.5.0

Solution

  • Use hc_size with height and width arguments:

    {r}
    h <- cars %>%
      hchart(.,"scatter",hcaes(x=speed,y=dist)) %>%
      hc_size(height=800,width=600)
    h
    

    Had the same problem and the comment from @jbkunst (kudos to them) to your question led me to this solution and it works - so I thought I'd post it as an answer.