Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add horizontal scroll bar for a ggplot plot in RMarkdown html

I have recently started using RMarkdown for reporting purposes. A specific report I am working on contains tables and plots for data acquired from many experiments carried out over the years - sort of updating and summarising data quickly.

While I have found a way to add scrollbars/scroll boxes for tables (by using Kable) and code chunk outputs, I have not been able to add scrollbars for plots. Most of plots are not big and its not an issue, but for one/two plots there are many categories and I need the plot to not resize when browser window size is changed or make the width of the entire page huge. Ideally, if possible, it should be of a specific size and in a scrollbox of fixed width.

Here is an example of the kind of plot I am trying to do. Any suggestions are welcome!

---
title: "Add horizontal scrol"
author: "KTy"
date: "9/21/2018"
output: html_document
---

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

## R Markdown
### Want to add horizontal scroll bar around a plot

```{r rnorm_box_violin}
set.seed(2300)
xdf1 <- data.frame(  var1 = rnorm(  10000 , mean = 5000 , sd = 10) , str1 = rep("a0",10000)  )

for ( x in 10:50 ){
  n <- sample(x = c(10,0.1) , size = 1)
  xdf2 <- data.frame( var1 = rnorm(  x*n*1000 , mean = 5000+(x/2) , sd = 10) , str1 = rep(paste0("a",x),x*n*1000))
  xdf1 <- rbind(xdf1,xdf2)
  }

plot1 <- ggplot(  data = xdf1  , aes( x = str1 , y = var1  ))  + 
  geom_violin(fill='grey90', scale = 'count', colour = 'grey70') + 
   geom_boxplot( width = 0.2 ,  alpha = 0.1 , colour = 'grey30')+
  theme_bw()+
  theme(axis.text.x =  element_text(angle = 45, hjust = 1 ,vjust = 1))
```
Produces this plot:
```{r plot_it , echo = FALSE, width = 20 , height = 7}
plot1
```

I am using RStudio on Mac. I hope what I am asking makes sense, please leave comments if anythings is not clear and I can try to explain further. Cheers, Thank you!

like image 302
ktyagi Avatar asked Sep 21 '18 16:09

ktyagi


People also ask

How to create a horizontal bar chart using Ggplot2 in R?

To create a horizontal bar chart using ggplot2 package, we need to use coord_flip () function along with the geom_bar and to add the labels geom_text function is used. These two functions of ggplot2 provides enough aesthetic characteristics to create the horizontal bar chart and put the labels at inside end of the bars.

Can I use ggplot2 graphics in R Markdown?

Therefore, ggplot2 graphics are often included in my R Markdown documents. Features of both packages are highly flexible and you CAN always get what you want ! But if you are just starting out, getting what you want can be cumbersome. In this post, I share with you some tips found over time.

How to create a horizontal bar chart in Python?

It’s very easy to create a horizontal bar chart.You just need to add the code coord_flip () after your bar chart code. This section contains best data science and self-development resources to help you on your path.

How to change the theme of a ggplot2 plot?

ggplot2 theme manages how your graphic looks like. All elemements can be changed through the theme () function but there also are pre-configured. The default theme used by ggplot2 is theme_gray () but I often switch for theme_bw () (for black and white ). p + labs (title = "Thème par défaut" ) p + theme_bw () + labs (title = "Avec theme_bw ()")


1 Answers

You can add custom CSS to your knitr document:

    ...
    plot1 <- ggplot(  data = xdf1  , aes( x = str1 , y = var1  ))  + 
    geom_violin(fill='grey90', scale = 'count', colour = 'grey70') + 
    geom_boxplot( width = 1 ,  alpha = 0.1 , colour = 'grey30')+
    theme_bw()+
    theme(axis.text.x =  element_text(angle = 45, hjust = 1 ,vjust = 1))
```
<style>
  .superbigimage{
      overflow-x:scroll;
      white-space: nowrap;
  }

  .superbigimage img{
     max-width: none;
  }


</style>


This produces the plot with a special css class
<div class="superbigimage">
```{r plot_it , echo = FALSE, fig.width=20,fig.height=3}
plot1
```
</div>


This produces the plot with the default settings
```{r plot_it2 , echo = FALSE, fig.width=20,fig.height=3}
plot1
```

Image for the lazy:

enter image description here

like image 164
Joost Avatar answered Sep 21 '22 10:09

Joost