gpt4 book ai didi

r - 在 flexdashboard 中自动调整 ggplot2 图的大小

转载 作者:行者123 更新时间:2023-12-04 17:58:52 25 4
gpt4 key购买 nike

我有一个只有一个框架的 flexdashboard。现在我遇到了两个问题:

首先,如何更改框架标题(“示例”)的大小?

其次,仪表板会在浏览器中自动调整大小。但是,ggplot2 图没有。我如何告诉 flexdashboard 自动调整此图的大小?

---
title: "Resize ggplot2 Plots in FlexDashboard"
output:
flexdashboard::flex_dashboard:
storyboard: true
theme: lumen
social: menu
source: embed
---

```{r setup, include=FALSE}
require(flexdashboard)
require(ggplot2)
df <- data.frame(year = c("2013", "2014", "2015", "2013", "2014", "2015", "2013", "2014", "2015"),
cat = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
freqA = c(100, 100, 110, 80, 80, 90, 90, 90, 100),
freqB = c(50, 50, 55, 40, 40, 45, 45, 45, 50))
```

### Example

```{r}
ggplot(df, aes(x = year, weight = freqA)) +
geom_bar(position="dodge", alpha = .2, width=.5) +
# add and overlay elements
geom_bar(aes(x = year, weight = freqB, fill = cat),
position = "dodge", width=.5) +
scale_fill_manual(values = c("#6baed6", "#fb6a4a", "#238b45")) +
# add hlines for waffle-design
geom_hline(yintercept=seq(0, 120, by = 10), col = 'white') +
# facet display - 1 row, 3 columns
facet_grid(. ~ cat) +
# delete labels of x- and y-axis
xlab("") + ylab("") +
# blank background and now grids and legend
theme(panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.background = element_blank(), legend.position = "none",
axis.text.x = element_text(angle = 0, hjust = 0.5))
```


***

Problem:

- How can I tell flex dashboard to automatically resize the ggplot2 Plot?

- The plot should fill the whole space!

最佳答案

feder80,我只是从“flexdashboard”开始,但是使用列而不是 Storyboard 布局怎么样?然后您可以设置列的大小,它似乎对大小调整更敏感(但它不会拉伸(stretch)图表到每个角落)。

参见:

这些页面注意到“默认情况下,仪表板中的 2 级 Markdown 标题 (----------------) 定义列,每个图表垂直堆叠专栏。”

请注意有特殊的 mobile layout defaults如果您在尺寸受限的较小屏幕上查看此内容。

代码

---
title: "Resize ggplot2 Plots in FlexDashboard v2"
output:
flexdashboard::flex_dashboard:
theme: lumen
social: menu
source: embed
---

```{r setup, include=FALSE}
require(flexdashboard)
require(ggplot2)
df <- data.frame(year = c("2013", "2014", "2015", "2013", "2014", "2015", "2013", "2014", "2015"),
cat = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
freqA = c(100, 100, 110, 80, 80, 90, 90, 90, 100),
freqB = c(50, 50, 55, 40, 40, 45, 45, 45, 50))
```

Column1
-------

### Example


```{r}


ggplot(df, aes(x = year, weight = freqA)) +
geom_bar(position="dodge", alpha = .2, width=.5) +
# add and overlay elements
geom_bar(aes(x = year, weight = freqB, fill = cat),
position = "dodge", width=.5) +
scale_fill_manual(values = c("#6baed6", "#fb6a4a", "#238b45")) +
# add hlines for waffle-design
geom_hline(yintercept=seq(0, 120, by = 10), col = 'white') +
# facet display - 1 row, 3 columns
facet_grid(. ~ cat) +
# delete labels of x- and y-axis
xlab("") + ylab("") +
# blank background and now grids and legend
theme(panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.background = element_blank(), legend.position = "none",
axis.text.x = element_text(angle = 0, hjust = 0.5))

```


Column2{data-width=200}
-------

Problem:

- How can I tell flex dashboard to automatically resize the ggplot2 Plot?

- The plot should fill the whole space!

使用不同浏览器窗口大小的图片

它不是很明显,但是你可以从右边的文字中看出这些是不同数量的像素宽。

Size 1

Maximised window

关于r - 在 flexdashboard 中自动调整 ggplot2 图的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37868548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com