让乳胶进入R图
我想使用让乳胶进入R图,r,latex,plot,R,Latex,Plot,我想使用base/lattice组合或与ggplot2组合,为R中的绘图元素(例如:标题、轴标签、注释等)添加LaTeX排版 问题: 是否有办法使用这些包将LaTeX放入绘图中,如果有,是如何做到的 如果没有,是否需要额外的软件包来实现这一点 例如,在Python matplotlib中,通过text.usetex包编译LaTeX,如下所述: 在R中是否有类似的程序生成此类绘图?您可以从R生成tikz代码: 您可以从R生成tikz代码: 以下命令正确地使用LaTeX绘制标题,这是从中窃取的:
base/lattice
组合或与ggplot2
组合,为R
中的绘图元素(例如:标题、轴标签、注释等)添加LaTeX
排版
问题:
- 是否有办法使用这些包将
放入绘图中,如果有,是如何做到的李>LaTeX
- 如果没有,是否需要额外的软件包来实现这一点
Python matplotlib
中,通过text.usetex
包编译LaTeX
,如下所述:
在
R
中是否有类似的程序生成此类绘图?您可以从R生成tikz代码:
您可以从R生成tikz代码: 以下命令正确地使用LaTeX绘制标题,这是从中窃取的:
plot(1, main=expression(beta[1]))
有关详细信息,请参见?plotmath
。以下命令正确地使用LaTeX绘制标题:
plot(1, main=expression(beta[1]))
有关详细信息,请参见
?plotmath
。以下是使用ggplot2
的示例:
q <- qplot(cty, hwy, data = mpg, colour = displ)
q + xlab(expression(beta +frac(miles, gallon)))
q下面是一个使用ggplot2
的示例:
q <- qplot(cty, hwy, data = mpg, colour = displ)
q + xlab(expression(beta +frac(miles, gallon)))
q几年前,我通过输出到.fig格式,而不是直接输出到.pdf;编写包含latex代码的标题,并使用fig2ps或fig2pdf创建最终图形文件。我不得不这样做的设置打破了R2.5;如果我必须再做一次,我会转而研究tikz,但无论如何,我会在这里把它作为另一个潜在的选择
关于我如何使用swave的注释如下:几年前,我通过输出到.fig格式而不是直接输出到.pdf;编写包含latex代码的标题,并使用fig2ps或fig2pdf创建最终图形文件。我不得不这样做的设置打破了R2.5;如果我必须再做一次,我会转而研究tikz,但无论如何,我会在这里把它作为另一个潜在的选择
关于我如何使用Swave的笔记在这里:这里是我自己实验室报告中的一些内容
tickzDevice
exportstikz
images forLaTeX
- 注意,在某些情况下,
“\\”
变为“\”
,“$”
变为“$\”
,如下R代码:“$z\\frac{a}{b}$”->“$\z\frac{a}{b}$\”
- 此外,xtable还将表导出到latex代码
守则:
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
library(重塑2)
图书馆(plyr)
图书馆(GG2)
图书馆(systemfit)
图书馆(xtable)
需要(图形)
需要(tikzDevice)
setwd(“~/DataFolder/”)
Lab5p9这是我自己实验室报告中的一些内容
tickzDevice
exportstikz
images forLaTeX
- 注意,在某些情况下,
“\\”
变为“\”
,“$”
变为“$\”
,如下R代码:“$z\\frac{a}{b}$”->“$\z\frac{a}{b}$\”
- 此外,xtable还将表导出到latex代码
守则:
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
library(重塑2)
图书馆(plyr)
图书馆(GG2)
图书馆(systemfit)
图书馆(xtable)
需要(图形)
需要(tikzDevice)
setwd(“~/DataFolder/”)
Lab5p9这里有一个很酷的函数,它允许您使用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)
express这里有一个很酷的函数,它允许您使用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)
express我只有一个解决办法。您可以首先生成一个eps文件,然后使用工具eps2pgf将其转换回pgf。请参见我有一个解决办法。您可以首先生成一个eps文件,然后使用工具eps2pgf将其转换回pgf。请参见包含一个TeX
函数,用于将LaTeX公式转换为R的plotmath表达式。可以在任何可以输入数学注释的位置使用它,例如轴标签、图例标签和常规文本
例如:
x包含一个TeX
函数,用于将LaTeX公式转换为R的plotmath表达式。可以在任何可以输入数学注释的位置使用它,例如轴标签、图例标签和常规文本
例如:
xhh演示(plotmath)很有趣,也是很好的东西,所以必须通过plotmath的语法重新解释数学符号?这似乎是浪费时间,特别是如果你有一个复杂的表达。这就是为什么我喜欢matplotlib自己编译LaTeX的能力。有什么东西可以利用LaTeX生成plotmath语法吗?我不知道。RWiki上有一篇关于让latex使用ggplot2的有趣帖子:演示(plotmath)很有趣,也很好,所以数学符号必须通过plotmath的语法重新解释?这似乎是浪费时间,特别是如果你有一个复杂的表达。这就是为什么我喜欢matplotlib自己编译LaTeX的能力。有什么东西可以利用LaTeX生成plotmath语法吗?我不知道。RWiki上有一篇关于让latex使用ggplot2的有趣帖子:我刚刚发现该软件包已从CRAN中删除。该软件包似乎仍在r-forge中可用。此外,它在这里是可用的:我刚刚发现该软件包已从CRAN中删除。看来该软件包在r-forge仍然可用。此外,这里还提供了:不幸的是,它几乎没有乳胶的特性。现在的情况如何?我认为R3.1.1有了一点改进。不幸的是,它没有