2009-09-08 102 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