Rstudio Markdown中文输出PDF文件

7 篇文章 0 订阅
  1. 注意首先安装R的rticles文档模板包
  2. Rstudio Markdown中文输出PDF文件:需要下载安装Tex文档编译器(texlive 或MikTex), 也可以运行以下代码,安装谢益辉提供的代码来解决tex文档编译问题:
install.packages('rmarkdown')
install.packages('tinytex')
library(tinytex) 
install_tinytex( )    #生成pdf文件的编译器--国内下载极慢!!!
#这个方法快,网上或github.com查找下载TinyTeX-1.zip,安装去掉下一行的‘#’注释即可。
#tinytex:::install_prebuilt("C:/Users/czliu/Downloads/TinyTeX-1.zip")   
install.packages("rticles")   #各种文档格式(杂志、书刊)
install.packaes("pandoc")     #文档格式转换包
  1. 点击Rstudio菜单 file ->newfile->R Markdown 弹出对话框 选择 From Template ->CTex Documents,新建Rmarkdown文档。

拷贝以下代码到Rstudio运行:

---
title: "回归、散点图、改变字体、公式输入、直方图、饼图绘制 "
author:

  • czliutz
    documentclass: ctexart
    keywords:
  • 中文
  • R Markdown
    output:
    rticles::ctex:
    fig_caption: yes
    number_sections: yes
    toc: yes
    ---

Rstudio Markdown中文输出PDF文件:需要安装Tex文档编译器(texlive 或MikTex), 或以下代码:

注意安装R的rticles文档模板包
点击Rstudio菜单 file ->newfile->R Markdown 弹出对话框 选择 From Template ->CTex Documents

options(digits = 4)
fit = lm(dist ~ speed, data = cars)
coef(summary(fit))
b = coef(fit)

上面回归方程中的斜率是r b[2],完整的回归方程为: Y = ‘ r b [ 1 ] ‘ + ‘ r b [ 2 ] ‘ x Y = `r b[1]` + `r b[2]`x Y=rb[1]+rb[2]x

画图当然也是木有问题的啦,想画就说嘛,不说我怎么知道你想画呢?

par(mar = c(4, 4, .1, .1), las = 1)
plot(cars, pch = 19)
abline(fit, col = 'red')

改变字体方法

\fangsong 仿宋, CJK 等价命令

\songti 宋体, CJK 等价命令

\heiti 黑体, CJK 等价命令

\kaishu 楷书, CJK 等价命令

公式输入

x 2 + y = z x^2 + \sqrt{y} = z x2+y =z

x y z = ( 1 + e x ) − 2 x y w x^{y^z}=(1+{\rm e}^x)^{-2xy^w} xyz=(1+ex)2xyw

layout(matrix(c(1 ,  1 ,  1 ,2 ,  3 ,  4 ,2 ,  3 ,  4) , nr = 3 ,  byrow = T))
hist(rnorm(25) ,col="VioletRed")
hist(rnorm(25) ,col="VioletRed")
hist(rnorm(25) ,col="VioletRed")
hist(rnorm(25) ,col="VioletRed")
op <-par( bg ="light blue")
x <-seq(0.2*pi ,len =51)
plot(x ,sin(x ) , type ="o", bg=par("bg"))
points(x ,sin(x ) , pch = 21 , cex  =1.5 ,bg="red")

R统计分析示例

example(t.test)
example(chisq.test)
example(lm)
example(aov)

饼图

par(mfrow=c(2,2))
slice<-c(10,12,4,16,8)
lbls <- c("US","UK","Australia","Germany","France")
pie(slice,labels = lbls,main = "Simple Pie Chart")
pct <- round(slice/sum(slice)*100)
lbls2 <- paste(lbls,' ',pct,"%",sep = " ")
pie(slice,labels = lbls2,col=rainbow(length(lbls2)),main = "Pie Chart with Percentages")

library(plotrix)
pie3D(slice,labels=lbls,explode=0.1,main="3D Pie Chart")

mytable <- table(state.region)
lbls3 <- paste(names(mytable),"\n",mytable,sep=" ")
pie(mytable,labels = lbls3,main = "Pie Chart from a table\n (with sample size)")


  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值