R Markdown:如何使文本围绕图形浮动?
R Markdown: How do I make text float around figures?
我用 R 制作了一个流程图,并将其包含在我的 R Markdown 文件中。
现在的情况:
代码:
```{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")
```
我想要什么:
- 我要文字用图左边的space
- 我希望图形标题与图形的中心对齐。现在标题在页面中间,忽略图形是否居中、右对齐或左对齐。
我怎样才能实现这些目标?
编辑 1:我想编织成 pdf。
编辑 2:根据评论中的要求,我的页面现在看起来如何:
据我所知,您可以在 markdown 文档中嵌入 HTML 代码,因此如果您要编写 HTML,您可以这样做:
用 div
标签将单元格包裹成你想要的对齐样式(不再需要对齐单元格),至于标题,你可以在底部添加一个带有标题的透明框作为
中的文本
<div style="float:right">
```{r flowchart-data, echo = FALSE, message = FALSE, fig.width = 7, fig.height = 6}
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))
(caption <- boxGrob(label = 'Ablauf der Datenverarbeitung', x=midx, y=.02, box_gp = gpar(alpha=0)))
connectGrob(Pharmazie, A, "L")
connectGrob(Finanzen, A, "L")
connectGrob(A, B, "N")
connectGrob(B, C, "N")
connectGrob(C, D, "N")
connectGrob(D, E, "N")
```
</div>
And this is the text that would go to the left of the chart.
有一个名为 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
包并设置这个块选项。这是一个可重现的例子:
---
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.
注意,这个解决方案很可能只适用于创建单个图的块。应该可以扩展到一个包含几个数字的块。
我也为此苦苦挣扎,但为了 html-输出。
r 块中有一个参数为我解决了这个问题:
out.extra='style="float:right; padding:10px"'
我用 R 制作了一个流程图,并将其包含在我的 R Markdown 文件中。
现在的情况:
代码:
```{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")
```
我想要什么:
- 我要文字用图左边的space
- 我希望图形标题与图形的中心对齐。现在标题在页面中间,忽略图形是否居中、右对齐或左对齐。
我怎样才能实现这些目标?
编辑 1:我想编织成 pdf。
编辑 2:根据评论中的要求,我的页面现在看起来如何:
据我所知,您可以在 markdown 文档中嵌入 HTML 代码,因此如果您要编写 HTML,您可以这样做:
用 div
标签将单元格包裹成你想要的对齐样式(不再需要对齐单元格),至于标题,你可以在底部添加一个带有标题的透明框作为
<div style="float:right">
```{r flowchart-data, echo = FALSE, message = FALSE, fig.width = 7, fig.height = 6}
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))
(caption <- boxGrob(label = 'Ablauf der Datenverarbeitung', x=midx, y=.02, box_gp = gpar(alpha=0)))
connectGrob(Pharmazie, A, "L")
connectGrob(Finanzen, A, "L")
connectGrob(A, B, "N")
connectGrob(B, C, "N")
connectGrob(C, D, "N")
connectGrob(D, E, "N")
```
</div>
And this is the text that would go to the left of the chart.
有一个名为 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
包并设置这个块选项。这是一个可重现的例子:
---
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.
注意,这个解决方案很可能只适用于创建单个图的块。应该可以扩展到一个包含几个数字的块。
我也为此苦苦挣扎,但为了 html-输出。 r 块中有一个参数为我解决了这个问题:
out.extra='style="float:right; padding:10px"'