ggplot2 plot在flexdashboard中伸展并削减底部

时间:2017-01-17 17:34:30

标签: r ggplot2 shiny flexdashboard

我想在我的Shiny应用程序上显示一些不同的图表,将它们分成几个命名的部分(使用Markdown' s ###)。但是,我在这些部分内打印的图表的底部被切割。不仅如此,当我改变他们的data-height属性时,他们只是为了保证他们仍然会被切断。

enter image description here

enter image description here

我或许可以将数据高度值设置得足够大,以至于根本不会切割图像,但到那时它会变形很大。在调整其所在部分的大小时,如何保持绘图的大小相同?或者甚至更好,是否有可能使截面尺寸自动适合绘图尺寸?

---编辑:

---
title: "title"
author: "author"
date: "date"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: scroll
    logo: ""
    favicon: ""
    source_code: embed
    runtime: shiny
runtime: shiny
---

```{r setup, include=FALSE}
library (tidyverse)
```

# Tab 1

## Column

### Section 1

```{r echo=FALSE}
# Data processing

inputPanel (
    # inputs
)

renderPlot ({
    # Data processing

    step = 0.05
    max = step * ceiling(max(retention_rate$high) / step)
    min = step * floor(min(retention_rate$low) / step)

    ggplot (retention_rate,
            aes (x = dsi, y = median,
                 ymin = low, ymax = high,
                 colour = ab_group, fill = ab_group)) +
        theme (panel.background = element_rect (fill = 'white'),
               panel.grid.major = element_line (colour = 'grey90', size = 0.20),
               panel.grid.minor = element_line (colour = 'grey90', size = 0.10),
               plot.title = element_text (lineheight = 1.15),
               axis.title.y = element_text (angle = 0),
               axis.title = element_text (size = 10),
               text = element_text (size = 10),
               legend.title = element_text (size = 10),
               strip.text = element_text (size = 10, angle = 0),
               plot.caption = element_text (hjust = 0.5, size = 9)) +
        geom_vline (xintercept = c (1, 3, 7, 14, 28),
                    colour = 'gray80', linetype = 2, size = 0.4) +
        geom_line () +
        geom_ribbon (aes (colour = NULL), alpha = 0.2) +
        scale_x_continuous (breaks = seq (0, max (retention_rate$dsi), 5)) +
        scale_y_continuous (limits = c  (min, max),
                            breaks = seq (min, max, step),
                            labels = sprintf ('%.0f %%', 100 * seq (min, max, step))) +
        scale_colour_brewer (palette = 'Dark2') +
        scale_fill_brewer (palette = 'Dark2') +
        facet_grid (source~country) +
        labs(x = '',
             y = '',
             colour = '',
             fill = '',
             title = '',
             subtitle = '')
})
```

### Days Active

```{r echo=FALSE}
# Data processing

inputPanel (
    # inputs
)

renderPlot ({
    # Data processing

    step = 0.5
    max = step * ceiling(max(da$high) / step)
    min = 0

    ggplot (da, aes (x = '', y = median,
                     ymin = low, ymax = high,
                     colour = ab_group, fill = ab_group)) +
        theme (panel.background = element_rect (fill = 'white'),
               panel.grid.major = element_line (colour = 'grey90', size = 0.20),
               panel.grid.minor = element_line (colour = 'grey90', size = 0.10),
               plot.title = element_text (lineheight = 1.15),
               axis.title.y = element_text (angle = 0),
               axis.title = element_text (size = 10),
               text = element_text (size = 10),
               legend.title = element_text (size = 10),
               strip.text = element_text (size = 10, angle = 0),
               plot.caption = element_text (hjust = 0.5, size = 9)) +
        geom_col (aes (colour = NULL), position = 'dodge', alpha = 0.60, width = 2 / 3) +
        geom_errorbar (position = position_dodge (width = 2 / 3), width = 1 / 3) +
        geom_text (position = position_dodge (width = 2 / 3),
                   aes (label = sprintf ('%.2f', median)), #hjust = - 1 / (nrow (da) - 1),
                   vjust = -1) +
        scale_y_continuous (limits = c (min, max),
                            breaks = seq (min, max, step)) +
        scale_colour_brewer (palette = 'Dark2') +
        scale_fill_brewer (palette = 'Dark2') +
        facet_grid (source~country) +
        labs (x = '',
              y = '',
              fill = '', colour = '',
              title = '',
              subtitle = '')
})
```

1 个答案:

答案 0 :(得分:0)

我无法重现您的示例,因为我没有retain_rate,所以我使用了mtcars数据,将renderPlot height设置为1000来模拟裁剪。

我在库(miniUI)中使用 miniContentPanel ,在上图中使用 scrollable = TRUE 。现在你有一个垂直滚动条,不像下面的情节。

enter image description here

---
title: "title"
author: "author"
date: "date"
output: 
  flexdashboard::flex_dashboard:
  orientation: columns
vertical_layout: scroll
logo: ""
favicon: ""
source_code: embed
runtime: shiny
---

```{r, setup, include=FALSE}
library (tidyverse)
library(miniUI)

```

# Tab 1

## Column

### Section 1

```{r, echo=FALSE}
# Data processing

inputPanel (
  # inputs
)

## Upper plot with miniContentPanel and scrollable = TRUE
miniContentPanel(
  renderPlot ({
    ggplot(mtcars,aes(x=mpg,y=cyl))+
      geom_point()
  },height=1000),
  scrollable = TRUE)

```

### Days Active

```{r, echo=FALSE}
# Data processing

inputPanel (
  # inputs
)

## lower plot without miniContentPanel ----
renderPlot({
  ggplot(mtcars,aes(x=gear,y=wt))+
    geom_point()},
height=1000)

```