2017-01-17 32 views
0

我想在我的Shiny應用程序中顯示幾個不同的圖,並將它們分成小命名段(使用Markdown的###)。但是,我在這些部分內打印的情節都會削減底部。不僅如此,當我改變它們的屬性時,它們只是爲了保證它們仍然會被切割。ggplot2 plot在flexdashboard中延伸並削減底部

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 = '') 
}) 
``` 
+0

你所說的 「最小重複的例子,」 是什麼意思?代碼示例或? –

+0

你可能想看看:http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – HubertL

+1

好吧,我會這樣做,並將其添加到但是我是否應該從中得出結論:這樣拉伸的圖形不是一個已知解決方案的常見問題?我也懷疑它與數據有什麼關係,因爲問題發生在不同的數據集甚至兩種不同的繪圖類型上。這似乎是應該從代碼本身推導出來的東西。 –

回答

0

我無法重現你的例子,因爲我沒有retention_rate,所以我用mtcars數據,renderPlot高度設置爲1000,模擬種植。

我用miniContentPanel庫(miniUI)與在上面的曲線滾動= 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) 

```