如何在RMarkdown html中为ggplot图添加水平滚动条 [英] How to add horizontal scroll bar for a ggplot plot in RMarkdown html

查看:181
本文介绍了如何在RMarkdown html中为ggplot图添加水平滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用RMarkdown进行报告.我正在处理的一份特定报告包含表格和图表,这些表格和图表用于说明这些年来从许多实验中获得的数据-一种快速更新和汇总数据的方法.

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.

虽然我找到了一种为表格添加滚动条/滚动框(通过使用Kable)和代码块输出的方法,但我却无法为绘图添加滚动条.大多数图都不大,也不是问题,但是对于一个/两个图而言,有很多类别,我需要在更改浏览器窗口大小或使整个页面的宽度变大时不要调整图的大小.理想情况下,如果可能的话,它应该具有特定的大小并位于固定宽度的滚动框中.

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
```

我在Mac上使用RStudio.我希望我的要求是有道理的,如果有任何不清楚的地方,请留下评论,我可以尝试进一步解释.干杯,谢谢!

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!

推荐答案

您可以将自定义CSS添加到您的knitr文档中:

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
```

惰性图片:

这篇关于如何在RMarkdown html中为ggplot图添加水平滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆