如何修复 knitr 生成的 pdf 和 html 中绘图的不一致显示
How to fix inconsistent display of plot in pdf and html generated by knitr
我正在使用 knitr 包在 R markdown 中编写文档,必须以 HTML 格式提供在线查看,以 pdf 格式提供离线阅读。如 html 和 pdf 文档中所示,绘图的输出存在一些不一致。
- 条形图图例在 pdf 中显示不正确
- 饼图图例与 pdf 中的图重叠
items <- c('Food', 'Clothing', 'House Rent', 'Education',
'Litigation', 'Conventional Needs', 'Miscellaneous')
familyA <-c(24,4,4,3,2,1,2)
familyB <-c(60,14,16,6,10,6,8)
df = data.frame(items, familyA, familyB)
familyAPerc <- prop.table(familyA) * 100
familyBPerc <- prop.table(familyB) * 100
df <- cbind.data.frame(items, familyA, familyAPerc, familyB, familyBPerc)
subdf <- df[, c(3,5)]
par(xpd=T, mar=c(5,4,1.4,0.2))
barplot(as.matrix(subdf), width=c(0.4, 1.2), legend.text = df$items, cex.main=0.6,
args.legend = list(x="bottom", ncol=4, cex=0.6, bty='n', inset=-0.2))
将插图更改为 -0.3 修复了 pdf 中的输出,但图例在 html 中被裁剪了。
x <- c(50, 30, 20,15,35)
labels <- c("food","clothing","house rent","fuel & light", "miscellaneous")
piepercent<- round(100*x/sum(x), 1)
pie(x, labels = piepercent, main = "Pie Diagram",col = rainbow(length(x)))
legend("topright", labels, cex = 0.8, fill = rainbow(length(x)))
整个 R 项目在 this github repo
上可用
如何在不花费数小时的情况下在 html 和 pdf 输出中获得正确的绘图显示?请帮忙
它没有包含在你的问题中,但在你引用的 Github 存储库中,你有这个 YAML:
---
title: "Presentation of Data"
output:
pdf_document:
keep_tex: yes
fig_width: 4
fig_height: 4
fig_caption: yes
toc: yes
html_document:
toc: yes
---
这会将 PDF 的宽度和高度设置为 4 英寸,并在 HTML 中将其保留为默认值(即 7 x 7 英寸)。将它们设置为相同的大小,数字将看起来相同。
如果 7 x 7 太大,您可以设置 fig.width=7, out.width="57%", fig.height=7, out.height="57%"
,绘图将以全尺寸绘制,然后缩小到更小的尺寸。据我所知,您必须在块选项中执行此操作,而不是在 YAML 中,但您可以使用
在初始块中将这些值设置为默认值
knitr::opts_chunk$set(fig.width=7, out.width="57%", fig.height=7, out.height="57%")
在设置块的开头。 (我选择 "57%"
将 7 英寸缩小到 4 英寸。不同的尺寸选择不同的百分比。)
我正在使用 knitr 包在 R markdown 中编写文档,必须以 HTML 格式提供在线查看,以 pdf 格式提供离线阅读。如 html 和 pdf 文档中所示,绘图的输出存在一些不一致。
- 条形图图例在 pdf 中显示不正确
- 饼图图例与 pdf 中的图重叠
items <- c('Food', 'Clothing', 'House Rent', 'Education',
'Litigation', 'Conventional Needs', 'Miscellaneous')
familyA <-c(24,4,4,3,2,1,2)
familyB <-c(60,14,16,6,10,6,8)
df = data.frame(items, familyA, familyB)
familyAPerc <- prop.table(familyA) * 100
familyBPerc <- prop.table(familyB) * 100
df <- cbind.data.frame(items, familyA, familyAPerc, familyB, familyBPerc)
subdf <- df[, c(3,5)]
par(xpd=T, mar=c(5,4,1.4,0.2))
barplot(as.matrix(subdf), width=c(0.4, 1.2), legend.text = df$items, cex.main=0.6,
args.legend = list(x="bottom", ncol=4, cex=0.6, bty='n', inset=-0.2))
将插图更改为 -0.3 修复了 pdf 中的输出,但图例在 html 中被裁剪了。
x <- c(50, 30, 20,15,35)
labels <- c("food","clothing","house rent","fuel & light", "miscellaneous")
piepercent<- round(100*x/sum(x), 1)
pie(x, labels = piepercent, main = "Pie Diagram",col = rainbow(length(x)))
legend("topright", labels, cex = 0.8, fill = rainbow(length(x)))
整个 R 项目在 this github repo
上可用如何在不花费数小时的情况下在 html 和 pdf 输出中获得正确的绘图显示?请帮忙
它没有包含在你的问题中,但在你引用的 Github 存储库中,你有这个 YAML:
---
title: "Presentation of Data"
output:
pdf_document:
keep_tex: yes
fig_width: 4
fig_height: 4
fig_caption: yes
toc: yes
html_document:
toc: yes
---
这会将 PDF 的宽度和高度设置为 4 英寸,并在 HTML 中将其保留为默认值(即 7 x 7 英寸)。将它们设置为相同的大小,数字将看起来相同。
如果 7 x 7 太大,您可以设置 fig.width=7, out.width="57%", fig.height=7, out.height="57%"
,绘图将以全尺寸绘制,然后缩小到更小的尺寸。据我所知,您必须在块选项中执行此操作,而不是在 YAML 中,但您可以使用
knitr::opts_chunk$set(fig.width=7, out.width="57%", fig.height=7, out.height="57%")
在设置块的开头。 (我选择 "57%"
将 7 英寸缩小到 4 英寸。不同的尺寸选择不同的百分比。)