将图的高度设置为在rmarkDown中的数据行数量等于相邻的表高度通过功能
我正在使用MTCARS数据在RmarkDown中的附带图和表创建。
---
title: "document"
author: "Maral Dorri"
date: 'May 2022'
output:
html_document
---
我创建一个列并在右侧添加表:
<div class = "row">
<div class = "col-md-3">
```{r}
raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg) %>%
mutate(links = paste(.$id, "And <a href = 'https://www.cars.com//'>here</a>"))
tibble(
name = raw_dat$id,
link = paste(raw_dat$mpg, "And <a href = 'https://www.cars.com//'>here</a>")) %>%
mutate(link = map(link, gt::html)) %>%
gt
```
</div>
然后我创建另一列并在左侧打印绘图。
<div class = "col-md-9">
```{r, fig.height=5.5}
ggplot(raw_dat, aes(factor(id, rev(id)), mpg)) +
geom_point() +
coord_flip() +
theme(plot.margin = margin(0.6, unit = "cm"))
```
</div>
</div>
结果为:
我知道我可以手动设置图形的高度大小与右侧的表相同,但是我想自动制作此功能,因为对于我的实际数据,它将定期更改。因此,用于图的高度的功能应根据表中的数据行数来进行。
所需的输出将在右侧的表中的每一行与左侧图的Y轴标签对齐,如图所示(手动完成,不是完美对齐)
I am using the mtcars data to create a side by side plot and table in Rmarkdown.
---
title: "document"
author: "Maral Dorri"
date: 'May 2022'
output:
html_document
---
I create a column and add the table on the right:
<div class = "row">
<div class = "col-md-3">
```{r}
raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg) %>%
mutate(links = paste(.$id, "And <a href = 'https://www.cars.com//'>here</a>"))
tibble(
name = raw_dat$id,
link = paste(raw_dat$mpg, "And <a href = 'https://www.cars.com//'>here</a>")) %>%
mutate(link = map(link, gt::html)) %>%
gt
```
</div>
Then I create another column and print the plot on the left
<div class = "col-md-9">
```{r, fig.height=5.5}
ggplot(raw_dat, aes(factor(id, rev(id)), mpg)) +
geom_point() +
coord_flip() +
theme(plot.margin = margin(0.6, unit = "cm"))
```
</div>
</div>
The results are:
I know I can set the height of the figure manually and capture the same size as the table on the right, but I want to make this automatic since for my actual data, it will be changing periodically. So the function used for the height of the plot should be in terms of number of rows of data in the table.
The desired output would line up each row of the data in the table on the right with the y axis labels of the plot on the left, as shown (done manually, and not perfectly aligned)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新...
好吧,这将起作用,直到绘图扩展到绘图边距超过表的标头行高(约15行)的程度,
这就是样式和JS。
这就是我的RMD中的样子。
更新
我没有意识到您希望标签能够在情节和表之间对齐。对此很抱歉。此更新的JS将创建所需的效果。
最初写的,
如果您想要
col-md-3
和col-md-9
的确切高度,则可以使用JS 。我要指出的第一件事是,主要容器的
最大宽度
是一个问题。我真的建议您要么不设置这一点,要么更改价值。我认为它设置为960 PX。您可以更改带有样式的主要容器最大宽度
属性。确保列的高度相同:
Update... again
Alright this will work until the plot has expanded to the point where the plot margin has scaled beyond the height of the table's header row (about 15 rows)
This is the styles and JS.
Here's how this is in my RMD altogether.
Update
I didn't realize that you wanted the labels to align between the plot and the table. Sorry about that. This updated JS will create the desired effect.
Originally wrote
If you want the exact height of
col-md-3
andcol-md-9
to be the same, you could use JS.The first thing I'll point out is that the
max-width
of the main container is a problem. I really suggest that you either unset this or change the value. I think it's set to 960 px. You can change the main containermax-width
property with styles.Ensuring that the columns are the same height:
bstfun
软件包为此提供了一种不错的自动方法。 (请注意,软件包依赖于其他各种软件包,例如magick
和webshot
。)如果可以近似,则可以使用表格和轴名为大致对齐:
源数据
The
bstfun
package offers a nice automatic approach for this. (Note the package relies on a variety of other packages, likemagick
andwebshot
.)If it's ok to be approximate, you can get the table and axis names roughly aligned using:
Source data