2009-09-08 61 views
99

我想LaTeX排版添加到情节元素R(例如:标题,轴标签,注释等)使用以下任一base/latticeggplot2组合。获得乳胶成R图解

问题:

  • 有没有办法让LaTeX到使用这些包地块,如果是的话,它怎么办?
  • 如果没有,是否有额外的软件包需要完成这个。

例如,在编译Python matplotlib经由text.usetexLaTeX如这里讨论:http://www.scipy.org/Cookbook/Matplotlib/UsingTex

是否有通过这样的试验田可以R可以产生类似的处理?

+0

本教程可能适合你(为我创造奇迹:)):https://www.r-bloggers.com/latex-in-r-graphs/ – 2016-08-25 07:40:08

回答

15

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

+9

这个包现在在CRAN上。 http://cran.r-project.org/web/packages/tikzDevice/ – 2009-09-13 04:28:48

+0

我刚刚发现包已从CRAN中删除。 – 2013-11-05 19:02:57

+0

看来这个软件包仍然可以在r-forge上使用。此外,它可在这里:https://github.com/Sharpie/RTikZDevice – Mica 2013-12-13 17:00:14

28

由于从here被盗,以下命令正确使用乳胶绘制标题:

plot(1, main=expression(beta[1])) 

详情请参阅?plotmath

+11

有趣的,也是好的东西与演示(plotmath) 所以数学符号必须通过plotmath的语法重新解释?这似乎是对时间的光荣浪费,尤其是如果你有涉及LaTeX表达的话。 这就是为什么我喜欢matplotlib自己编译LaTeX的能力。有什么可以采取LaTeX和生成plotmath语法? – DrewConway 2009-09-08 19:20:08

+0

不是我所知道的。在RWiki有一篇关于让乳胶与ggplot2一起工作的有趣帖子:http://wiki.r-project.org/rwiki/doku.php?id=tips:graphics-ggplot2:mathematicalannotations – 2009-09-08 20:06:48

41

下面是一个使用ggplot2一个例子:

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

alt text http://i31.tinypic.com/10z7n7d.png

+12

不幸的是,这并没有近LaTeX的特点。 – 2014-09-11 06:32:57

+0

现在情况如何?我认为它在R 3.1.1上有所提升。 – 2016-10-30 10:09:28

2

我几年前做这个通过输出到.FIG格式,而不是直接为.pdf;您可以编写包括乳胶代码的标题,并使用fig2ps或fig2pdf来创建最终的图形文件。我不得不这样做的设置违反了R 2.5;如果我不得不再做一次,我会考虑tikz,但是无论如何我都会把它包括在内,作为另一个潜在的选择。

我对我是怎么做到的使用Sweave音符在这里:http://www.stat.umn.edu/~arendahl/computing

4

下面是从我自己的实验报告什么的。

  • 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 
4

这是一个很酷的函数,可以让你使用plotmath功能,但可以将表达式存储为字符模式的对象。这使您可以使用粘贴或正则表达式函数以编程方式操作它们。我不使用ggplot,但它也应该在那里工作:

express <- function(char.expressions){ 
     return(parse(text=paste(char.expressions,collapse=";"))) 
    } 
    par(mar=c(6,6,1,1)) 
    plot(0,0,xlim=sym(),ylim=sym(),xaxt="n",yaxt="n",mgp=c(4,0.2,0), 
     xlab="axis(1,(-9:9)/10,tick.labels,las=2,cex.axis=0.8)", 
     ylab="axis(2,(-9:9)/10,express(tick.labels),las=1,cex.axis=0.8)") 
    tick.labels <- paste("x >=",(-9:9)/10) 
    # this is what you get if you just use tick.labels the regular way: 
    axis(1,(-9:9)/10,tick.labels,las=2,cex.axis=0.8) 
    # but if you express() them... voila! 
    axis(2,(-9:9)/10,express(tick.labels),las=1,cex.axis=0.8) 
22

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

例如:

x <- seq(0, 4, length.out=100) 
alpha <- 1:5 
plot(x, xlim=c(0, 4), ylim=c(0, 10), xlab='x', ylab=latex2exp('\\alpha 
    x^\\alpha\\text{, where }\\alpha \\in \\text{1:5}'), type='n') 
for (a in alpha) 
    lines(x, a*x^a, col=a) 
legend('topleft', legend=latex2exp(sprintf("\\alpha = %d", alpha)), lwd=1, col=alpha) 

产生this plot