适应 Quarto revealjs 中的空间列布局的图表。

huangapple go评论98阅读模式
英文:

Fit plot to space column layout in Quarto revealjs

问题

我有一段代码和一张地图,我想将它们并排显示,所以我使用了output-location: column。然而,地图显得非常小,与代码相比。有没有办法让它适应宽度和高度?

我尝试过通过减小边距来实现:par(mar = c(0, 0, 0, 0)),但没有任何变化。我还尝试过使用整数值来设置out-widthout-height(百分比在revealjs中不起作用),但这会破坏宽高比。

有什么想法?


格式:revealjs

一个标题

  1. #| output-location: column
  2. #| echo: true
  3. library(spData)
  4. library(sf)
  5. library(ggplot2)
  6. #par(mar = c(0, 0, 0, 0))
  7. ggplot() +
  8. geom_sf(data = nz) +
  9. theme_minimal()

适应 Quarto revealjs 中的空间列布局的图表。

注:这似乎是sf对象的常见问题:https://stackoverflow.com/questions/67687029/eliminate-outer-margin-ggplot2-geom-sf。

英文:

I have a code and a map that I would like to plot on the side, so I use output-location: column. The map however, appears very small compared to the code. Is there any way to make it fit the width and height?

I've tried by reducing the margins: par(mar = c(0, 0, 0, 0)), but it does not change anything. I've also tried out-width and out-height with integers (percentages do not work in revealjs), but this destroys the aspect ratio.

Any ideas?

  1. ---
  2. format: revealjs
  3. ---
  4. ## A title
  5. ```{r}
  6. #| output-location: column
  7. #| echo: true
  8. library(spData)
  9. library(sf)
  10. library(ggplot2)
  11. #par(mar = c(0, 0, 0, 0))
  12. ggplot() +
  13. geom_sf(data = nz) +
  14. theme_minimal()
  15. ```

适应 Quarto revealjs 中的空间列布局的图表。

Note: this seems to be a common problem with sf objects: https://stackoverflow.com/questions/67687029/eliminate-outer-margin-ggplot2-geom-sf.

答案1

得分: 2

Option 01

一种选择可能是保存地图,然后使用 knitr::plot_crop 来获取地图的图像,其中额外的白色边距将被裁剪掉,然后使用 magick::read_image 渲染图像。

我还使用了一些 CSS 来微调地图的高度,以获得漂亮的输出。

  1. ---
  2. title: "Fit plot to space column layout"
  3. format: revealjs
  4. ---
  5. ## A title
  6. ```{r}
  7. #| output-location: column
  8. #| echo: true
  9. #| classes: map
  10. library(spData)
  11. library(sf)
  12. library(ggplot2)
  13. library(magick)
  14. map <- ggplot() +
  15. geom_sf(data = nz) +
  16. coord_sf(expand = F) +
  17. theme_minimal()
  18. ggsave("map.png")
  19. . <- knitr::plot_crop("map.png")
  20. print(
  21. image_read("map.png"),
  22. info = FALSE
  23. )
  1. .reveal .map img {
  2. height: 80vh;
  3. padding-left: 2em;
  4. }

适应 Quarto revealjs 中的空间列布局的图表。

  1. Option 02
  2. 您可以使用 knitr 钩子 [`knitr::hook_pdfcrop`](https://bookdown.org/yihui/rmarkdown-cookbook/crop-plot.html) 一次完成选项 01 的所有步骤。
  3. 首先通过在 `knit_hooks$set()` 中设置它来启用它,然后在代码块中使用 `crop: true`
  4. ```{r}
  5. ---
  6. title: "Fit plot to space column layout"
  7. format: revealjs
  8. ---
  9. ## A title
  10. ```{r setup, include=FALSE}
  11. knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)
  1. #| output-location: column
  2. #| echo: true
  3. #| crop: true
  4. #| classes: map
  5. library(spData)
  6. library(sf)
  7. library(ggplot2)
  8. ggplot() +
  9. geom_sf(data = nz) +
  10. coord_sf(expand = F) +
  11. theme_minimal()
  1. .reveal .map img {
  2. height: 80vh;
  3. width: auto !important;
  4. padding-left: 2em;
  5. }

[![figure for the second option][2]][2]

  1. [1]: https://i.stack.imgur.com/0BgEN.png
  2. [2]: https://i.stack.imgur.com/ONipw.png
  3. <details>
  4. <summary>英文:</summary>
  5. ## Option 01
  6. One option could be saving the map and then using `knitr::plot_crop` to get an image of the map where extra white margins will be cropped off and then render the image with `magick::read_image`.
  7. I have also used a bit CSS to tune the map height a little bit and get a nice output.
  8. ~~~
  9. ---
  10. title: &quot;Fit plot to space column layout&quot;
  11. format: revealjs
  12. ---
  13. ## A title
  14. ```{r}
  15. #| output-location: column
  16. #| echo: true
  17. #| classes: map
  18. library(spData)
  19. library(sf)
  20. library(ggplot2)
  21. library(magick)
  22. map &lt;- ggplot() +
  23. geom_sf(data = nz) +
  24. coord_sf(expand = F) +
  25. theme_minimal()
  26. ggsave(&quot;map.png&quot;)
  27. . &lt;- knitr::plot_crop(&quot;map.png&quot;)
  28. print(
  29. image_read(&quot;map.png&quot;),
  30. info = FALSE
  31. )
  1. .reveal .map img {
  2. height: 80vh;
  3. padding-left: 2em;
  4. }
  1. &lt;hr&gt;
  2. [![map with removed border][1]][1]
  3. ## Option 02
  4. You can use the knitr hooks [`knitr::hook_pdfcrop`](https://bookdown.org/yihui/rmarkdown-cookbook/crop-plot.html) to do all of the steps of option 01 in one go.
  5. At first enable it by setting it in `knit_hooks$set()` and then use `crop: true` in the code chunk.

title: "Fit plot to space column layout"
format: revealjs

A title

  1. knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)
  1. #| output-location: column
  2. #| echo: true
  3. #| crop: true
  4. #| classes: map
  5. library(spData)
  6. library(sf)
  7. library(ggplot2)
  8. ggplot() +
  9. geom_sf(data = nz) +
  10. coord_sf(expand = F) +
  11. theme_minimal()
  1. .reveal .map img {
  2. height: 80vh;
  3. width: auto !important;
  4. padding-left: 2em;
  5. }
  1. &lt;hr&gt;
  2. [![figure for the second option][2]][2]
  3. [1]: https://i.stack.imgur.com/0BgEN.png
  4. [2]: https://i.stack.imgur.com/ONipw.png
  5. </details>
  6. # 答案2
  7. **得分**: 2
  8. 你可以使用 `fig-asp` 选项如下所示:
  9. ```markdown
  10. ---
  11. format:
  12. revealjs
  13. ---
  14. ## 标题
  15. ```{r}
  16. #| output-location: column
  17. #| echo: true
  18. #| fig-asp: 1
  19. library(spData)
  20. library(sf)
  21. library(ggplot2)
  22. #par(mar = c(0, 0, 0, 0))
  23. ggplot() +
  24. geom_sf(data = nz) +
  25. theme_minimal()
  26. ```
  27. 输出:
  28. [![enter image description here][1]][1]
  29. ```
  30. [1]: https://i.stack.imgur.com/XyQai.png
  31. <details>
  32. <summary>英文:</summary>
  33. Maybe you could use the `fig-asp` option like this:
  34. ---
  35. format:
  36. revealjs
  37. ---
  38. ## A title
  39. ```{r}
  40. #| output-location: column
  41. #| echo: true
  42. #| fig-asp: 1
  43. library(spData)
  44. library(sf)
  45. library(ggplot2)
  46. #par(mar = c(0, 0, 0, 0))
  47. ggplot() +
  48. geom_sf(data = nz) +
  49. theme_minimal()
  50. ```
  51. Output:
  52. [![enter image description here][1]][1]
  53. [1]: https://i.stack.imgur.com/XyQai.png
  54. </details>
  55. # 答案3
  56. **得分**: 1
  57. 你可以使用 `max-width`,但我希望有更好的方法:
  58. ```
  59. ---
  60. format: revealjs
  61. ---
  62. <style>
  63. .reveal img {
  64. max-width: 150%;
  65. overflow-x: hidden;
  66. }
  67. </style>
  68. ## 一个标题
  69. ```{r}
  70. #| output-location: column
  71. #| echo: true
  72. library(spData)
  73. library(sf)
  74. library(ggplot2)
  75. ggplot() +
  76. geom_sf(data = nz) +
  77. theme_minimal()
  78. ```
  79. [![enter image description here][1]][1]
  80. ```
  81. [![在这里输入图片描述][1]][1]
  82. <details>
  83. <summary>英文:</summary>
  84. You could use `max-width`, but I hope that there are better ways:
  85. ---
  86. format: revealjs
  87. ---
  88. &lt;style&gt;
  89. .reveal img {
  90. max-width: 150%;
  91. overflow-x: hidden;
  92. }
  93. &lt;/style&gt;
  94. ## A title
  95. ```{r}
  96. #| output-location: column
  97. #| echo: true
  98. library(spData)
  99. library(sf)
  100. library(ggplot2)
  101. ggplot() +
  102. geom_sf(data = nz) +
  103. theme_minimal()
  104. ```
  105. [![enter image description here][1]][1]
  106. [1]: https://i.stack.imgur.com/CfPqL.png
  107. </details>

huangapple
  • 本文由 发表于 2023年6月6日 00:20:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定