ggplot2 - Flexdashboard 无法在同一降价上呈现 ggplotly 和 ggplot 对象
问题描述
我在这里有一个基本的可重现示例,我认为这可能只是一个包限制。我想知道我是不是做错了什么?它们都可以单独绘制,但是当组合在同一个降价中时,仪表板无法正确呈现。
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
runtime: shiny
---
```{r setup, include=FALSE}
library(tidyverse)
library(plotly)
library(albersusa)
state_sf <- usa_sf("aeqd")
state_dat <- data.frame(state = c("Washington", "Wyoming","Texas","California"), pct = c(0.3,0.5,0.8,0.1))
state_map <- state_sf %>%
left_join(state_dat, by = c("name" = "state"))
```
Test
=====================================
Sidebar {.sidebar data-width=200}
-------------------------------------
Testing
Row
-----------------------------------------------------------------------
###Plotly
```{r graph 1, fig.height=4, fig.width=6}
#Symptoms by state last week===================================================
ggplotly(
ggplot(data = state_map) +
geom_sf(aes(fill=pct))
)
```
###Bar
```{r graph 2, fig.height=4, fig.width=3}
ggplot(data=state_dat) +
geom_col(aes(state,pct,fill=pct))
```
解决方案
如果您正在使用,则runtime: shiny
需要renderX()
为每种类型的绘图对象使用正确类型的 Shiny 函数才能正确显示。我不知道为什么只有一个情节块(w/o renderX()
)有效,但有两个打破了它。
### Plotly
```{r graph_1, fig.height=4, fig.width=3}
#Symptoms by state last week
renderPlotly({
ggplotly(
ggplot(data = state_map) +
geom_sf(aes(fill=pct))
)
})
```
### Bar
```{r graph_2, fig.height=4, fig.width=3}
renderPlot({
ggplot(data=state_dat) +
geom_col(aes(state,pct,fill=pct))
})
```
推荐阅读
- python - 我如何制作矩阵列表数字 1-something
- javascript - Stencil JS - 使用 Prop 值启动状态变量?
- symfony - 在我的实体中使用标记类 - Symfony 4
- reactjs - 反应: event.target.value 如何在以下工作
- docker - Cosmos DB 模拟器 SSL 如何在 docker compose 中工作?
- c# - 从参数中获取字段名
- python - Django视图混乱,两个视图显示相同的内容
- github-api - Github API /用户标识符属性
- linux - 从“dd”恢复备份(softRAID HDD 可能有问题)
- ruby-on-rails - Sidekiq 在使用多个队列时忽略并发性