install.packages('rmarkdown')
install.packages('tinytex')
library(tinytex)
install_tinytex( )
install.packages("rticles")
---
title: "回归、散点图、改变字体、公式输入、直方图、饼图绘制 "
author:
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)")