ggplot2 绘图在 flexdashboard 中延伸并切到底部
ggplot2 plot stretches in flexdashboard and cuts bottom
我想在我的 Shiny 应用程序中显示几个不同的图,将它们分成小的命名部分(使用 Markdown 的 ###
)。但是,我在这些部分中打印的图底部被切掉了。不仅如此,当我更改它们的 data-height
属性时,它们会拉伸以确保它们仍会被切割。
我也许可以设置一个足够大的数据高度值,它根本不会切割图像,但到那时它会极度变形。如何在调整绘图所在部分的大小时保持绘图大小相同?或者更好的是,是否可以使部分大小自动适合绘图大小?
--- 编辑:
---
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 = '')
})
```
我无法重现你的例子,因为我没有retention_rate,所以我使用了 mtcars 数据,将 renderPlot 高度设置为 1000 来模拟裁剪。
我使用了 library(miniUI) 中的 miniContentPanel,上层 scrollable = TRUE阴谋。现在你有一个不同于下图的垂直滚动条。
---
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)
```
我想在我的 Shiny 应用程序中显示几个不同的图,将它们分成小的命名部分(使用 Markdown 的 ###
)。但是,我在这些部分中打印的图底部被切掉了。不仅如此,当我更改它们的 data-height
属性时,它们会拉伸以确保它们仍会被切割。
我也许可以设置一个足够大的数据高度值,它根本不会切割图像,但到那时它会极度变形。如何在调整绘图所在部分的大小时保持绘图大小相同?或者更好的是,是否可以使部分大小自动适合绘图大小?
--- 编辑:
---
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 = '')
})
```
我无法重现你的例子,因为我没有retention_rate,所以我使用了 mtcars 数据,将 renderPlot 高度设置为 1000 来模拟裁剪。
我使用了 library(miniUI) 中的 miniContentPanel,上层 scrollable = TRUE阴谋。现在你有一个不同于下图的垂直滚动条。
---
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)
```