gpt4 book ai didi

r - 在 Markdown 中使用长数字

转载 作者:行者123 更新时间:2023-12-01 05:53:52 25 4
gpt4 key购买 nike

我在 Markdown 报告中显示长数字。

这些很长,因为它们使用 ggplot2::facet_wrap所以他们的高度取决于数据,这不是恒定的。

我可以设置figure.height块的参数,但后来它被修复了,我的报告看起来很糟糕。他们有办法解决这个问题吗?

例子 :

---
title: "title"
author: "author"
date: '`r Sys.Date()`'
output: html_document
---

```{r, figure.height=40}
library(dplyr)
library(ggplot2)
iris %>%
mutate_at("Sepal.Length",cut, 5) %>%
mutate_at("Sepal.Width",cut,2) %>%
group_by_at(c(1,2,5)) %>%
summarize_at("Petal.Length",mean) %>%
ggplot(aes(Species, Petal.Length)) +
geom_col() +
facet_wrap(Sepal.Length ~ Sepal.Width,ncol=2)
```

最佳答案

一起去n * single_height想法:您可以使用块选项 eval.after使fig.widthfig.height选项将在评估块的其余部分后评估,然后使用 ggplot_build拉开 ggplot 对象并确定分面中使用的行数和列数。

例如:

---
author: "author"
date: '`r Sys.Date()`'
output: html_document
---

```{r setup}
library(dplyr)
library(ggplot2)
library(knitr)

FACET_HEIGHT = 3.4
FACET_WIDTH = 5

opts_chunk$set(out.width = "80%",
out.height = "80%",
eval.after = c("fig.height", "fig.width"))
```

For the example we'll have one basic plot to which we will set different facets.
```{r}
g <-
iris %>%
mutate_at("Sepal.Length",cut, 5) %>%
mutate_at("Sepal.Width",cut,2) %>%
group_by_at(c(1,2,5)) %>%
summarize_at("Petal.Length",mean) %>%
ggplot(aes(Species, Petal.Length)) +
geom_col()
```

A graphic with two columns
```{r fig1, fig.height = FACET_HEIGHT * max(ggplot_build(g1)$layout$layout$ROW), fig.width = FACET_WIDTH * max(ggplot_build(g1)$layout$layout$COL)}
g1 <- g + facet_wrap(Sepal.Length ~ Sepal.Width, ncol = 2)
g1
```


A graphic with two rows
```{r fig2, fig.height = FACET_HEIGHT * max(ggplot_build(g2)$layout$layout$ROW), fig.width = FACET_WIDTH * max(ggplot_build(g2)$layout$layout$COL)}
g2 <- g + facet_wrap(Sepal.Length ~ Sepal.Width, nrow = 2)
g2
```

生成的 html 的屏幕截图是:

enter image description here

需要对图像宽度和高度进行一些微调,但这应该是一个很好的起点。

关于r - 在 Markdown 中使用长数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51066311/

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