楼主: janyiyi
1630 1

LaTeX in R graphs [推广有奖]

  • 3关注
  • 17粉丝

讲师

27%

还不是VIP/贵宾

-

威望
0
论坛币
3206 个
通用积分
5056.6800
学术水平
539 点
热心指数
537 点
信用等级
538 点
经验
10157 点
帖子
300
精华
2
在线时间
90 小时
注册时间
2010-10-3
最后登录
2024-4-6

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

A nice post was recently published on the rsnippets blog, about the tikzDevice R package. This package is – indeed – awesome. Even if it has been removed from the CRAN website. Of course, it can be download from the archive folder, on http://cran.r-project.org/…, but also (for a more recent version)  on http://download.r-forge.r-project.org/…. But first, it is necessary to install the following package.

  1. > install.packages("filehash")
复制代码


Then, we download on of the tikzDevice.zip files, and load it, e.g. using (on Mac)



Then, we can load the library

  1. > library("tikzDevice")
复制代码

If we want to use nice LaTeX formulas, it might be necessary to upload some (LaTeX) libraries and to specify the encoding format

  1. > "options(tikzMetricPackages = c("\\usepackage[utf8]{inputenc}",
  2. + "\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}", "\\usepackage{amssymb}"))
复制代码

(this is detailed, e.g. in  http://yihui.name/…), then, we write a code to plot a graph. The idea is to produce a tex file which contains the graph, or more precisely which will produce a pdf graph when we compile it. We start with

  1. > tikz("normal-dist.tex", width = 8, height = 4,
  2. + standAlone = TRUE,
  3. + packages = c("\\usepackage{tikz}",
  4. + "\\usepackage[active,tightpage,psfixbb]{preview}",
  5. + "\\PreviewEnvironment{pgfpicture}",
  6. + "\\setlength\\PreviewBorder{0pt}",
  7. + "\\usepackage{amssymb}"))
复制代码

We will produce a 8×4 graph. The graph is the following,

  1. > u=seq(-3,3,by=.01)
  2. > plot(u,dnorm(u),type="l",axes=FALSE,xlab="",ylab="",col="white")
  3. > axis(1)
  4. > I=which((u>=0)&(u<=1))
  5. > polygon(c(u[I],rev(u[I])),c(dnorm(u)[I],rep(0,length(I))),col="red",border=NA)
  6. > lines(u,dnorm(u),lwd=2,col="blue")
复制代码

We can add text (or TeX based text)

  1. > text(-1.5, dnorm(-1.5)+.17, "$\\textcolor{blue}{X\\sim\\mathcal{N}(0,1)}$", cex = 1.5)
  2. > text(1.75, dnorm(1.75)+.25,
  3. + "$\\textcolor{red}{\\mathbb{P}(X\\in[0,1])=\\displaystyle{\\int_0^1 \\varphi(x)dx}}$", cex = 1.5)
复制代码

And we end the file with a standard

  1. > dev.off()
复制代码

This will produce a .tex file. If we compile this file, we can generate a pdf file, that can be inserted in lecture notes, slides or articles,




Nice, isn’t it ?


Arthur CharpentierArthur Charpentier, professor in Montréal, in Actuarial Science. Former professor-assistant at ENSAE Paristech, associate professor at Ecole Polytechnique and assistant professor in Economics at Université de Rennes 1.  Graduated from ENSAE, Master in Mathematical Economics (Paris Dauphine), PhD in Mathematics (KU Leuven), and Fellow of the French Institute of Actuaries.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Graphs GRAPH LaTeX GRAP Late published recently awesome package website

已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
oliyiyi + 60 + 40 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 60  论坛币 + 40  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

本帖被以下文库推荐

沙发
oliyiyi 发表于 2019-4-30 16:33:44 |只看作者 |坛友微信交流群
谢谢分享

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 01:46