gpt4 book ai didi

r - ggplot 绘图中的字体大小与使用 knitr 的绘图有关

转载 作者:行者123 更新时间:2023-12-01 13:48:14 24 4
gpt4 key购买 nike

我是第一次使用 knitr,在 ggplot 绘图中遇到字体大小问题。这是一个示例图:

d <- ggplot(diamonds, aes(x = cut, y = clarity))
d + stat_sum(aes(label=..n..),geom="text",size=8)

在 knitr 中,我在 R markdown 中的一个 block 中有相同的情节:

---
title: "Untitled"
output: html_document
---
```{r, echo=FALSE}
library(ggplot2)
d <- ggplot(diamonds, aes(x = cut, y = clarity))
d + stat_sum(aes(label=..n..),geom="text",size=8)
```

该图在 RStudio 中或使用 ggsave() 保存时看起来不错。但是,生成的 knitr html 中绘图中的数字总体上和相对于绘图大小的字体大小要大得多:

ggsave image knitr image from html

在这个例子中,这并不重要,但在我的数据中,数字开始相互重叠/用完它们的单元格。

一个更复杂的问题是绘图是由一个包完成的,所以我不能轻易地更改 stat_sum 调用中的大小选项。

最佳答案

尝试调整 fig.heightfig.width:

---
title: "Untitled"
output: html_document
---
```{r, echo=FALSE,fig.height=10,fig.width=10}
library(ggplot2)
d <- ggplot(diamonds, aes(x = cut, y = clarity))
d + stat_sum(aes(label=..n..),geom="text",size=8)
```

如果不想让图形太大,可以调整out.heightout.width:

---
title: "Untitled"
output: html_document
---
```{r, echo=FALSE,fig.height=10,fig.width=10,out.height=600,out.width=600}
library(ggplot2)
d <- ggplot(diamonds, aes(x = cut, y = clarity))
d + stat_sum(aes(label=..n..),geom="text",size=8)
```

关于r - ggplot 绘图中的字体大小与使用 knitr 的绘图有关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34093160/

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