使用 pkg:sjPlot 函数创建生成部分斜体单元格的数据框
Creating a data frame that produces partially italicized cells with pkg:sjPlot functions
我正在尝试创建一个简单的数据 table,其中“Coral_taxon”列中的属名是斜体,而“spp.”是斜体。属名后面的部分不用大写。我尝试使用 expression() 函数为“Coral_taxon”的每一行编码,但没有成功。
sum_dat_final2 <- data.frame(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"),
Coral_taxon = c(expression(italic("Montipora"), " spp."), expression(italic("Pocillopora"), " spp."), expression(italic("Porites"), " spp."), expression(italic("Montipora"), " spp."), expression(italic("Pocillopora"), " spp."), expression(italic("Porites"), " spp.")),
Estimate = c("0.45 ± 0.13 %", "0.43 ± 0.18 %", "19.5 ± 2.6 %", "0.05 ± 0.03 %", "0.47 ± 0.3 %", "1.19 ± 0.56 %"))
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘"expression"’ to a data.frame
我猜您希望物种名称的后面紧跟文字 spp.
。所以不要在列表中使用表达式向量,而是为此目的构造正确的表达式。建议改用这种形式:
sum_dat_final2 <- list(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"),
Coral_taxon = expression( italic(Montipora)~ spp.,
italic(Pocillopora)~spp.,
italic(Porites)~spp.,
italic(Montipora)~ spp.,
italic(Pocillopora)~spp.,
italic(Porites)~spp.),
Estimate = c("0.45 ± 0.13 %", "0.43 ± 0.18 %", "19.5 ± 2.6 %", "0.05 ± 0.03 %", "0.47 ± 0.3 %", "1.19 ± 0.56 %"))
使用 M-- 的修改,这是 tab_df 调用的结果:
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style>
html, body { background-color: white; }
table { border-collapse:collapse; border:none; }
caption { font-weight: bold; text-align:left; }
td { }
.thead { border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; }
.tdata { padding:0.2cm; text-align:left; vertical-align:top; }
.arc { background-color:#f2f2f2; }
.summary { padding-top:0.1cm; padding-bottom:0.1cm; }
.summarydata { text-align:center; }
.fixedparts { font-weight:bold; text-align:left; }
.randomparts { font-weight:bold; text-align:left; padding-top:.8em; }
.zeroparts { font-weight:bold; text-align:left; padding-top:.8em; }
.simplexparts { font-weight:bold; text-align:left; padding-top:.8em; }
.lasttablerow { border-bottom: double; }
.firsttablerow { border-bottom:1px solid black; }
.firstsumrow { border-top:1px solid; }
.labelcellborder { border-bottom:1px solid; }
.depvarhead { text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; }
.leftalign { text-align:left; }
.centeralign { text-align:center; }
.firsttablecol { text-align:left; }
.footnote { font-style:italic; border-top:double black; text-align:right; }
.subtitle { font-weight: normal; }
.modelcolumn1 { }
.modelcolumn2 { }
.modelcolumn3 { }
.modelcolumn4 { }
.modelcolumn5 { }
.modelcolumn6 { }
.modelcolumn7 { }
.col1 { }
.col2 { }
.col3 { }
.col4 { }
.col5 { }
</style>
</head>
<body>
<table>
<tr>
<th class="thead firsttablerow firsttablecol col1">Site</th>
<th class="thead firsttablerow col2">Coral_taxon</th>
<th class="thead firsttablerow col3">Estimate</th>
</tr>
<tr>
<td class="tdata firsttablecol col1">Hanauma Bay</td>
<td class="tdata centeralign col2"><i>Montipora</i> spp.</td>
<td class="tdata centeralign col3">0.45 ± 0.13 %</td>
</tr>
<tr>
<td class="tdata firsttablecol col1">Hanauma Bay</td>
<td class="tdata centeralign col2"><i>Pocillopora</i> spp.</td>
<td class="tdata centeralign col3">0.43 ± 0.18 %</td>
</tr>
<tr>
<td class="tdata firsttablecol col1">Hanauma Bay</td>
<td class="tdata centeralign col2"><i>Porites</i> spp.</td>
<td class="tdata centeralign col3">19.5 ± 2.6 %</td>
</tr>
<tr>
<td class="tdata firsttablecol col1">Waikiki</td>
<td class="tdata centeralign col2"><i>Montipora</i> spp.</td>
<td class="tdata centeralign col3">0.05 ± 0.03 %</td>
</tr>
<tr>
<td class="tdata firsttablecol col1">Waikiki</td>
<td class="tdata centeralign col2"><i>Pocillopora</i> spp.</td>
<td class="tdata centeralign col3">0.47 ± 0.3 %</td>
</tr>
<tr>
<td class="tdata firsttablecol lasttablerow col1">Waikiki</td>
<td class="tdata centeralign lasttablerow col2"><i>Porites</i> spp.</td>
<td class="tdata centeralign lasttablerow col3">1.19 ± 0.56 %</td>
</tr>
</table>
</body></html>
sjPlot
库使用 HTML 标签。您可以使用 <i>Text</i>
进行斜体化。
df1 <- data.frame(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Waikiki", "Waikiki", "Waikiki"),
Coral_taxon = I(list("<i>Montipora</i> spp.",
"<i>Pocillopora</i> spp.",
"<i>Porites</i> spp.",
"<i>Montipora</i> spp.",
"<i>Pocillopora</i> spp.",
"<i>Porites</i> spp.")),
Estimate = c("0.45 <U+623C><U+3E31> 0.13 %", "0.43 <U+623C><U+3E31> 0.18 %",
"19.5 <U+623C><U+3E31> 2.6 %", "0.05 <U+623C><U+3E31> 0.03 %",
"0.47 <U+623C><U+3E31> 0.3 %", "1.19 <U+623C><U+3E31> 0.56 %"))
sjPlot::tab_df(df1)
运行 查看格式化的片段 table。
<table style="border-collapse:collapse; border:none;">
<tr>
<th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; text-align:left; ">
Site
</th>
<th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; ">
Coral_taxon
</th>
<th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; ">
Estimate
</th>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Hanauma Bay
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Montipora</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
0.45 ±0.13 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Hanauma Bay
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Pocillopora</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
0.43 ±0.18 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Hanauma Bay
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Porites</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
19.5 ±2.6 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Waikiki
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Montipora</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
0.05 ±0.03 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Waikiki
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Pocillopora</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
0.47 ±0.3 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; border-bottom: double; ">
Waikiki
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; border-bottom: double; ">
<i>Porites</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; border-bottom: double; ">
1.19 ±0.56 %
</td>
</tr>
</table>
我正在尝试创建一个简单的数据 table,其中“Coral_taxon”列中的属名是斜体,而“spp.”是斜体。属名后面的部分不用大写。我尝试使用 expression() 函数为“Coral_taxon”的每一行编码,但没有成功。
sum_dat_final2 <- data.frame(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"),
Coral_taxon = c(expression(italic("Montipora"), " spp."), expression(italic("Pocillopora"), " spp."), expression(italic("Porites"), " spp."), expression(italic("Montipora"), " spp."), expression(italic("Pocillopora"), " spp."), expression(italic("Porites"), " spp.")),
Estimate = c("0.45 ± 0.13 %", "0.43 ± 0.18 %", "19.5 ± 2.6 %", "0.05 ± 0.03 %", "0.47 ± 0.3 %", "1.19 ± 0.56 %"))
Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘"expression"’ to a data.frame
我猜您希望物种名称的后面紧跟文字 spp.
。所以不要在列表中使用表达式向量,而是为此目的构造正确的表达式。建议改用这种形式:
sum_dat_final2 <- list(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"),
Coral_taxon = expression( italic(Montipora)~ spp.,
italic(Pocillopora)~spp.,
italic(Porites)~spp.,
italic(Montipora)~ spp.,
italic(Pocillopora)~spp.,
italic(Porites)~spp.),
Estimate = c("0.45 ± 0.13 %", "0.43 ± 0.18 %", "19.5 ± 2.6 %", "0.05 ± 0.03 %", "0.47 ± 0.3 %", "1.19 ± 0.56 %"))
使用 M-- 的修改,这是 tab_df 调用的结果:
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style>
html, body { background-color: white; }
table { border-collapse:collapse; border:none; }
caption { font-weight: bold; text-align:left; }
td { }
.thead { border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; }
.tdata { padding:0.2cm; text-align:left; vertical-align:top; }
.arc { background-color:#f2f2f2; }
.summary { padding-top:0.1cm; padding-bottom:0.1cm; }
.summarydata { text-align:center; }
.fixedparts { font-weight:bold; text-align:left; }
.randomparts { font-weight:bold; text-align:left; padding-top:.8em; }
.zeroparts { font-weight:bold; text-align:left; padding-top:.8em; }
.simplexparts { font-weight:bold; text-align:left; padding-top:.8em; }
.lasttablerow { border-bottom: double; }
.firsttablerow { border-bottom:1px solid black; }
.firstsumrow { border-top:1px solid; }
.labelcellborder { border-bottom:1px solid; }
.depvarhead { text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; }
.leftalign { text-align:left; }
.centeralign { text-align:center; }
.firsttablecol { text-align:left; }
.footnote { font-style:italic; border-top:double black; text-align:right; }
.subtitle { font-weight: normal; }
.modelcolumn1 { }
.modelcolumn2 { }
.modelcolumn3 { }
.modelcolumn4 { }
.modelcolumn5 { }
.modelcolumn6 { }
.modelcolumn7 { }
.col1 { }
.col2 { }
.col3 { }
.col4 { }
.col5 { }
</style>
</head>
<body>
<table>
<tr>
<th class="thead firsttablerow firsttablecol col1">Site</th>
<th class="thead firsttablerow col2">Coral_taxon</th>
<th class="thead firsttablerow col3">Estimate</th>
</tr>
<tr>
<td class="tdata firsttablecol col1">Hanauma Bay</td>
<td class="tdata centeralign col2"><i>Montipora</i> spp.</td>
<td class="tdata centeralign col3">0.45 ± 0.13 %</td>
</tr>
<tr>
<td class="tdata firsttablecol col1">Hanauma Bay</td>
<td class="tdata centeralign col2"><i>Pocillopora</i> spp.</td>
<td class="tdata centeralign col3">0.43 ± 0.18 %</td>
</tr>
<tr>
<td class="tdata firsttablecol col1">Hanauma Bay</td>
<td class="tdata centeralign col2"><i>Porites</i> spp.</td>
<td class="tdata centeralign col3">19.5 ± 2.6 %</td>
</tr>
<tr>
<td class="tdata firsttablecol col1">Waikiki</td>
<td class="tdata centeralign col2"><i>Montipora</i> spp.</td>
<td class="tdata centeralign col3">0.05 ± 0.03 %</td>
</tr>
<tr>
<td class="tdata firsttablecol col1">Waikiki</td>
<td class="tdata centeralign col2"><i>Pocillopora</i> spp.</td>
<td class="tdata centeralign col3">0.47 ± 0.3 %</td>
</tr>
<tr>
<td class="tdata firsttablecol lasttablerow col1">Waikiki</td>
<td class="tdata centeralign lasttablerow col2"><i>Porites</i> spp.</td>
<td class="tdata centeralign lasttablerow col3">1.19 ± 0.56 %</td>
</tr>
</table>
</body></html>
sjPlot
库使用 HTML 标签。您可以使用 <i>Text</i>
进行斜体化。
df1 <- data.frame(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay",
"Waikiki", "Waikiki", "Waikiki"),
Coral_taxon = I(list("<i>Montipora</i> spp.",
"<i>Pocillopora</i> spp.",
"<i>Porites</i> spp.",
"<i>Montipora</i> spp.",
"<i>Pocillopora</i> spp.",
"<i>Porites</i> spp.")),
Estimate = c("0.45 <U+623C><U+3E31> 0.13 %", "0.43 <U+623C><U+3E31> 0.18 %",
"19.5 <U+623C><U+3E31> 2.6 %", "0.05 <U+623C><U+3E31> 0.03 %",
"0.47 <U+623C><U+3E31> 0.3 %", "1.19 <U+623C><U+3E31> 0.56 %"))
sjPlot::tab_df(df1)
运行 查看格式化的片段 table。
<table style="border-collapse:collapse; border:none;">
<tr>
<th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; text-align:left; ">
Site
</th>
<th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; ">
Coral_taxon
</th>
<th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; ">
Estimate
</th>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Hanauma Bay
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Montipora</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
0.45 ±0.13 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Hanauma Bay
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Pocillopora</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
0.43 ±0.18 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Hanauma Bay
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Porites</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
19.5 ±2.6 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Waikiki
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Montipora</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
0.05 ±0.03 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
Waikiki
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
<i>Pocillopora</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
0.47 ±0.3 %
</td>
</tr>
<tr>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; border-bottom: double; ">
Waikiki
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; border-bottom: double; ">
<i>Porites</i> spp.
</td>
<td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; border-bottom: double; ">
1.19 ±0.56 %
</td>
</tr>
</table>