gpt4 book ai didi

R Markdown : How do I make text float around figures?

转载 作者:行者123 更新时间:2023-12-03 23:21:20 28 4
gpt4 key购买 nike

我使用R制作了流程图,并将其包含在R Markdown文件中。

现在的样子:
enter image description here

编码:

```{r flowchart-data, echo = FALSE, message = FALSE, fig.cap = "Ablauf der Datenverarbeitung", fig.align = "right",  fig.width = 7, fig.height = 6, out.extra = 'trim = {0 1.1cm 0 0}, clip', out.width=".7\\textwidth"}
library(grid)
library(Gmisc)

# grid.newpage()
# set some parameters to use repeatedly
leftx <- .2
midx <- .5
rightx <- .8

myBoxGrob <- function(text, ...) {
boxGrob(label = text, bjust = "top", box_gp = gpar(fill = "lightgrey"), ...)
}

# create boxes
(Pharmazie <- myBoxGrob("Verbrauchsdaten von der\n Spitalpharmazie (Excel-Tabelle)", x=leftx, y=1, width = 0.36))
(Finanzen <- myBoxGrob("Belegzahlen vom Ressort\n Finanzen (Excel-Tabelle)", x=rightx, y=1, width = 0.36))
(A <- myBoxGrob("Import der Daten aus Excel ins\n Microsoft Access (Datenbanksoftware)", x=midx, y=0.83, width = 0.45))
(B <- myBoxGrob("Zusammenführen der Informationen\n und erstellen neuer, berechneter Tabellen", x=midx, y=.66, width = 0.45))
(C <- myBoxGrob("Export der neu erstellten Tabellen\n in Form von Excel-Tabellen", x=midx, y=.49, width = 0.45))
(D <- myBoxGrob("Import der neuen Tabellen in R", x=midx, y=.32, width = 0.45))
(E <- myBoxGrob("Berechnung und grafische Darstellung\n der Grafiken und Tabellen", x=midx, y=.19, width = 0.45))


connectGrob(Pharmazie, A, "L")
connectGrob(Finanzen, A, "L")
connectGrob(A, B, "N")
connectGrob(B, C, "N")
connectGrob(C, D, "N")
connectGrob(D, E, "N")
```

我想要的是:
  • 我希望文本使用图左侧的空格。
  • 我希望图形标题与图形的中心对齐。现在,标题位于页面的中间,而忽略图形是居中,右对齐还是左对齐。

  • 我该如何实现这些目标?

    编辑1:我想编织到pdf。

    编辑2:按照评论中的要求,我的页面现在看起来如何:
    How my page looks now

    最佳答案

    有一个名为fig.env的块选项,使用该选项可以从figure切换到marginfigure环境。
    不幸的是,可能的环境列表不包括wrapfigure。因此,我们将更改图块:

    defOut <- knitr::knit_hooks$get("plot")  # save the default plot hook 
    knitr::knit_hooks$set(plot = function(x, options) { # set new plot hook ...
    x <- defOut(x, options) # first apply the default hook
    if(!is.null(options$wrapfigure)) { # then, if option wrapfigure is given ...
    # create the new opening string for the wrapfigure environment ...
    wf <- sprintf("\\begin{wrapfigure}{%s}{%g\\textwidth}", options$wrapfigure[[1]], options$wrapfigure[[2]])
    x <- gsub("\\begin{figure}", wf, x, fixed = T) # and replace the default one with it.
    x <- gsub("{figure}", "{wrapfigure}", x, fixed = T) # also replace the environment ending
    }
    return(x)
    })

    这些评论应阐明我们在这里实际所做的事情。注意, wrapfigure的期望值是两个元素的列表。第一个告诉LaTeX将图形移到页面的任一侧。第二个元素告诉LaTeX包装图形的宽度。若要将宽度为 0.7\\textwidth的图形移到右侧,请设置 wrapfigure = list("R", 0.7)(您可能已经猜到了, L将其移到左侧)。
    现在我们要做的就是在YAML中包括 wrapfig包,并设置此chunk选项。这是一个可重现的示例:
    ---
    header-includes:
    - \usepackage{wrapfig}
    - \usepackage{lipsum}
    output:
    pdf_document:
    keep_tex: true
    ---

    ```{r, include = F}
    defOut <- knitr::knit_hooks$get("plot") # save the default plot hook
    knitr::knit_hooks$set(plot = function(x, options) { # set new plot hook ...
    x <- defOut(x, options) # first apply the default hook
    if(!is.null(options$wrapfigure)) { # then, if option wrapfigure is given ...
    # create the new opening string for the wrapfigure environment ...
    wf <- sprintf("\\begin{wrapfigure}{%s}{%g\\textwidth}", options$wrapfigure[[1]], options$wrapfigure[[2]])
    x <- gsub("\\begin{figure}", wf, x, fixed = T) # and replace the default one with it.
    x <- gsub("{figure}", "{wrapfigure}", x, fixed = T) # also replace the environment ending
    }
    return(x)
    })
    ```


    Vivamus vehicula leo a justo. Quisque nec augue. Morbi mauris wisi, aliquet vitae, dignissim eget, sollicitudin molestie, ligula. In dictum enim sit amet risus. Curabitur vitae velit eu diam rhoncus hendrerit. Vivamus ut elit. Praesent mattis ipsum quis turpis. Curabitur rhoncus neque eu dui. Etiam vitae magna. Nam ullamcorper. Praesent interdum bibendum magna. Quisque auctor aliquam dolor. Morbi eu lorem et est porttitor fermentum. Nunc egestas arcu at tortor varius viverra. Fusce eu nulla ut nulla interdum consectetuer. Vestibulum gravida.

    ```{r echo = F, warning = F, message = F, fig.width=7, fig.height = 6, out.width = ".7\\textwidth", fig.cap = "My Flowchart", fig.align="right", wrapfigure = list("R", .7)}
    plot(mpg ~ hp, data = mtcars)
    ```

    Morbi mattis libero sed est. Vivamus vehicula leo a justo. Quisque nec augue. Morbi mauris wisi, aliquet vitae, dignissim eget, sollicitudin molestie, ligula. In dictum enim sit amet risus. Curabitur vitae velit eu diam rhoncus hendrerit. Vivamus ut elit. Praesent mattis ipsum quis turpis. Curabitur rhoncus neque eu dui. Etiam vitae magna. Nam ullamcorper. Praesent interdum bibendum magna. Quisque auctor aliquam dolor. Morbi eu lorem et est porttitor fermentum. Nunc egestas arcu at tortor varius viverra. Fusce eu nulla ut nulla interdum consectetuer. Vestibulum gravida. Morbi mattis libero sed est.

    请注意,该解决方案很可能仅适用于创建单个图的块。应该可以将其扩展到包含多个图形的块。

    enter image description here

    关于R Markdown : How do I make text float around figures?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54100806/

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