---
title: "回归、散点图、改变字体、公式输入、直方图、饼图绘制 "
author:
- 刘铁柱
documentclass: ctexart
keywords:
- 中文
- R Markdown
output:
rticles::ctex:
fig_caption: yes
number_sections: yes
toc: yes
---
回归、散点图、改变字体、公式输入、直方图、饼图绘制
**请使用R markdown ctex文档模板**
Rstudio Markdown中文输出PDF文件:需要安装Tex文档编译器(texlive 、MikTex), 或以下代码:
```{r eval=FALSE}
library(tinytex)
install_tinytex()
install.packages("rticles")
注意安装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')
改变字体方法
\kaishu 事实证明我们可以理直气壮地通过XeLaTeX将中文R Markdown转化为PDF文档,麻麻再也不用担心我的论文满屏幕都是反斜杠,朕养完小白鼠之后终于不必先折腾三个小时LaTeX再开始写实验报告了:打开RStudio,菜单File > New File > R Markdown,然后从模板中选择CTeX Documents,搞定。
字体
\fangsong 仿宋, CJK 等价命令
\songti 宋体, CJK 等价命令
\heiti 黑体, CJK 等价命令
\kaishu 楷书, CJK 等价命令
公式输入
$ $ x^2 + \sqrt{y} = z $ $
$ $
x{yz}=(1+{\rm e}x){-2xy^w}
$ $
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)")