【问题标题】:How to increase height of Plot to occupy full height space in flexdashboard shiny in r?如何增加 Plot 的高度以在 r 中闪亮的 flexdashboard 中占据整个高度空间?
【发布时间】:2021-04-02 12:24:16
【问题描述】:

我正在使用 tidytuesday 联合国投票数据集并尝试通过nrows 调整 facet plot 以占据 flexdashboard 中的 full height情节,但它几乎没有利用一半的空间,使情节不那么明显。

另一种选择是我可以制作 5 个不同的绘图,但是当它可以使用 facet 一次性完成时,这将运行 代码 5 次

我也试过facet_gridpar(mfrow = c(1,1)),但都没有帮助。

代码:

library(flexdashboard)
library(shiny)
library(tidyverse)
library(scales)
library(glue)
library(countrycode)
library(tidytuesdayR)

remotes::install_github("davidsjoberg/ggstream") # for creating streamgraph

数据:

tt <- tt_load("2021-03-23")
unvotes <- tt$unvotes

head(unvotes)
   rcid country       country_code vote  vote_number date       amend
  <dbl> <chr>         <chr>        <chr>       <dbl> <date>     <dbl>
1     3 United States US           yes             1 1946-01-01     1
2     3 Canada        CA           no             -1 1946-01-01     1
3     3 Cuba          CU           yes             1 1946-01-01     1
4     3 Haiti         HT           yes             1 1946-01-01     1
5     3 Dominican Re~ DO           yes             1 1946-01-01     1
6     3 Mexico        MX           yes             1 1946-01-01     1

弹性仪表板

    ---
title: "UN Country Votes"
output:
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    theme: space
runtime: shiny
resource_files:
- .RData
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(scales)
library(glue)
library(countrycode)
library(ggstream)    
library(wesanderson)
```   

Trend {data-icon="fa-bar-chart"}
=====================================

Inputs {.sidebar}
-----------------------------------------------------------------------



Column {data-width=550}
-----------------------------------------------------------------------

### UN Vote Trend over the years

```{r}
# Space for other plot
```


Column {data-width=450}
-----------------------------------------------------------------------

### UN Vote Trend by Continents

```{r}

# par(mfrow = c(1,1))
 unvotes %>%
  mutate(continent = countrycode(country_code, "iso2c", "continent")) %>%
  # mutate_all(as.factor) %>%
  group_by(continent, years = year(date) , vote) %>%
  summarise(count = n(), .groups = "drop_last") %>%
  na.omit() %>%
  # mutate(pct = count/sum(count)) %>%


  ggplot(aes(x = years, y = count, fill = vote)) +
  ggstream::geom_stream(show.legend = FALSE) +
  geom_stream_label(aes(label = vote)) +
  scale_y_continuous(labels = comma, breaks = seq(-2000,2000, 500)) +
  scale_fill_manual(values = wes_palette("Darjeeling2")) +
  theme(panel.grid.major = element_blank()) +
  facet_wrap(~continent, nrow = 5) +

  labs(title = "World UN Voting trend over the years by continent",
       y = "", x = "",
       caption = "created by ViSa")
```

【问题讨论】:

    标签: r shiny flexdashboard facet-wrap


    【解决方案1】:

    使用renderPlot 包装您的情节,使其具有响应性:

    ---
    title: "UN Country Votes"
    output:
        flexdashboard::flex_dashboard:
        orientation: columns
    vertical_layout: fill
    theme: space
    runtime: shiny
    resource_files:
        - .RData
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(shiny)
    library(tidyverse)
    ```
    
    # Trend {data-icon="fa-bar-chart"}
    
    ## Inputs {.sidebar}
    
    ## Column {data-width="550"}
    
    ### UN Vote Trend over the years
    
    ```{r}
    # Space for other plot
    ```
    
    ## Column {data-width="450"}
    
    ### UN Vote Trend by Continents
    
    ```{r}
    
    # par(mfrow = c(1,1))
    renderPlot ({
    ggplot(iris) +
        geom_point(aes(Sepal.Length, Sepal.Width))
    })
    ```
    
    
    

    我用一个最小的例子简化了你的代码。

    【讨论】:

    • 谢谢@Iz100。我不知道renderPlot() 也能有所作为。感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 2021-10-14
    • 2018-08-14
    • 2021-10-17
    • 1970-01-01
    • 2020-06-04
    • 2012-08-23
    相关资源
    最近更新 更多