R Markdown rstudio回归、散点图、改变字体、公式输入、直方图、饼图绘制

27 篇文章 0 订阅
7 篇文章 0 订阅
---
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)")


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值