dmt*_*dmt 3 latex r ggplot2 knitr
我通常使用scalesR中的包来计算ggplots y轴上的计数数据百分比.但是,当我尝试在knitr中执行此操作时,由于绘图上的百分比符号,我收到错误:
Error in getMetricsFromLatex(TeXMetrics) :
TeX was unable to calculate metrics for the following string or character:
0%
我可以使用正常计数轴生存,但更喜欢%.有没有解决的办法?
这是一个可重复的示例(删除+ scale_y_continuous(label=percent)提供了一个可编译的示例):
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
<<packages, echo=F>>=
library(ggplot2)
library(scales)
@
<<chunkopts, echo=F>>=
opts_chunk$set(echo=F, cache=T, autodep=T, fig.width = 9/1.5, fig.height=5/1.5,
fig.align="center", dev="tikz", fig.pos="h!tbp", warning=F, message=F)
dep_auto()
@
<<mydata>>=
mydata <- data.frame(
X = letters[1:10],
Y = sample(c("yes", "no"), 100, replace = TRUE))
@
<<plot>>=
ggplot(mydata, aes(X, fill = Y)) + geom_bar(position = "fill") +
scale_y_continuous(label=percent)
@
\end{document}
Run Code Online (Sandbox Code Playgroud)
我正在使用TexShop编译我的.Rnw文件.
Ros*_*tev 11
问题是%乳胶中的特殊字符,它需要转义.尝试以下替代比例
library(stringr)
library(plyr)
latex_percent <- function (x) {
x <- plyr::round_any(x, scales:::precision(x)/100)
stringr::str_c(comma(x * 100), "\\%")
}
Run Code Online (Sandbox Code Playgroud)
并替换labels=percent为labels=latex_percent.