将LaTeX变成R图

Dre*_*way 116 plot latex r

我想补充LaTeX排版到地块元件R(例如:标题,轴标签,注释等)的使用任一组合base/lattice或与ggplot2.

问题:

  • 有没有办法LaTeX使用这些包进入图表,如果是这样,它是如何完成的?
  • 如果没有,是否需要额外的包来完成此任务.

例如,通过这里讨论的包进行Python matplotlib编译:http://www.scipy.org/Cookbook/Matplotlib/UsingTexLaTeXtext.usetex

是否有类似的过程可以生成这样的图R

Chr*_*ois 46

这是一个使用示例ggplot2:

q <- qplot(cty, hwy, data = mpg, colour = displ)
q + xlab(expression(beta +frac(miles, gallon)))
Run Code Online (Sandbox Code Playgroud)

替代文字http://i31.tinypic.com/10z7n7d.png

  • 不幸的是,这几乎没有LaTeX的功能. (16认同)

Chr*_*ois 35

这里被盗,以下命令正确使用LaTeX绘制标题:

plot(1, main=expression(beta[1]))
Run Code Online (Sandbox Code Playgroud)

有关?plotmath详细信息,请参阅

  • 有趣的,也是演示的好东西(plotmath)所以数学符号必须通过plotmath的语法重新解释?这似乎是浪费时间的光荣,特别是如果你有一个参与的LaTeX表达.这就是为什么我喜欢matplotlib编译LaTeX本身的能力.有什么东西可以采取LaTeX并生成plotmath语法? (12认同)

Ste*_*ari 28

该脚本包含一个TeX将LaTeX公式近似转换为R的plotmath表达式的函数.您可以在任何可以输入数学注释的地方使用它,例如轴标签,图例标签和一般文本.

例如:

x <- seq(0, 4, length.out=100)
alpha <- 1:5

plot(x, xlim=c(0, 4), ylim=c(0, 10), 
     xlab='x', ylab=TeX('$\\alpha  x^\\alpha$, where $\\alpha \\in 1\\ldots 5$'), 
     type='n', main=TeX('Using $\\LaTeX$ for plotting in base graphics!'))

invisible(sapply(alpha, function(a) lines(x, a*x^a, col=a)))

legend('topleft', legend=TeX(sprintf("$\\alpha = %d$", alpha)), 
       lwd=1, col=alpha)
Run Code Online (Sandbox Code Playgroud)

产生这个情节.

  • 我的意思是,LaTeX 字符串被翻译成 R 的plotmath 表达式——这意味着如果plotmath 不支持特定的LaTeX 符号,那么它要么不会被渲染,要么会通过组合可用的符号来渲染。 (2认同)

Mic*_*ica 15

您可以从R生成tikz代码:http://r-forge.r-project.org/projects/tikzdevice/

  • 这个包现在在CRAN上.http://cran.r-project.org/web/packages/tikzDevice/ (9认同)

N8T*_*TRO 6

这是我自己的实验室报告中的内容.

  • tickzDevice导出tikz图像LaTeX
  • 请注意,在某些情况下,"\\"变为"\""$"变为"$\"以下R代码:"$z\\frac{a}{b}$" -> "$\z\frac{a}{b}$\"

  • xtable也将表导出到乳胶代码

代码:

library(reshape2)
library(plyr)
library(ggplot2)
library(systemfit)
library(xtable)
require(graphics)
require(tikzDevice)

setwd("~/DataFolder/")
Lab5p9 <- read.csv (file="~/DataFolder/Lab5part9.csv", comment.char="#")

AR <- subset(Lab5p9,Region == "Forward.Active")

# make sure the data names aren't already in latex format, it interferes with the ggplot ~  # tikzDecice combo
colnames(AR) <- c("$V_{BB}[V]$", "$V_{RB}[V]$" ,  "$V_{RC}[V]$" , "$I_B[\\mu A]$" , "IC" , "$V_{BE}[V]$" , "$V_{CE}[V]$" , "beta" , "$I_E[mA]$")

# make sure the working directory is where you want your tikz file to go
setwd("~/TexImageFolder/")

# export plot as a .tex file in the tikz format
tikz('betaplot.tex', width = 6,height = 3.5,pointsize = 12) #define plot name size and font size

#define plot margin widths
par(mar=c(3,5,3,5)) # The syntax is mar=c(bottom, left, top, right).

ggplot(AR, aes(x=IC, y=beta)) +                                # define data set 
    geom_point(colour="#000000",size=1.5) +                # use points
    geom_smooth(method=loess,span=2) +                     # use smooth
    theme_bw() +                    # no grey background
    xlab("$I_C[mA]$") +                 # x axis label in latex format
    ylab ("$\\beta$") +                 # y axis label in latex format
    theme(axis.title.y=element_text(angle=0)) + # rotate y axis label
    theme(axis.title.x=element_text(vjust=-0.5)) +  # adjust x axis label down
    theme(axis.title.y=element_text(hjust=-0.5)) +  # adjust y axis lable left
    theme(panel.grid.major=element_line(colour="grey80", size=0.5)) +# major grid color
    theme(panel.grid.minor=element_line(colour="grey95", size=0.4)) +# minor grid color 
    scale_x_continuous(minor_breaks=seq(0,9.5,by=0.5)) +# adjust x minor grid spacing
    scale_y_continuous(minor_breaks=seq(170,185,by=0.5)) + # adjust y minor grid spacing
    theme(panel.border=element_rect(colour="black",size=.75))# border color and size

dev.off() # export file and exit tikzDevice function
Run Code Online (Sandbox Code Playgroud)