R基本图表| 散点图

R基本图表| 散点图

一、两个变量的散点图加回归

pdf("mygraph.pdf") #用来生成pdf文件,可以不要
 attach(mtcars)#导入该数据集
 plot(wt, mpg)#绘制散点图。x:wt y:mpg
 abline(lm(mpg~wt))#拟合直线
 title("Regression of MPG on Weight")#添加标题
 detach(mtcars)#撤下数据集 
dev.off()
dev.new()

这个指令用来在一次操作里在不同的窗口独立画不同图形

Patient response to two drugs at five dosage levels
剂量 A B

20

16 15
30 20 18
40 27 25
45 40 31
60 60 40

 

#数据输入方式是创建3个变量,然后通过多维向量的方式储存进去
dose<-c(20, 30, 40, 45, 60)
drugA<-c(16, 20, 27, 40, 60)
drugB<-c(15, 18, 25, 31, 40)

1)点图plot()

plot(x,y,type="b")
plot(dose,drugA,type="b")

R基本图表| 散点图

2)修改点图的一些特征

opar<-par(no.readonly=TRUE)# produces a list of current graphical settings that can be modified.The first statement makes a copy of the current settings.

par(lty=2,pch=17)#changes the default line type to dashed (lty=2) and the default symbol for plotting

plot(does, drugA,type="b", lty=2,pch=17)#或者最省事直接这样写

R基本图表| 散点图

关于plot()的特征值和对应变量形式的选取详情请参照R in action P51.有空补充,涉及颜色样式这样,我觉得没什么意思。用的时候再查就好了。

3)添加文本定制坐标轴和图例

plot(dose, drugA, type="b",
     col="red", lty=2, pch=2, lwd=2,
     main="Clinical Trials for Drug A",
     sub="This is hypothetical data",
     xlab="Dosage", ylab="Drug Response",
     xlim=c(0, 60), ylim=c(0, 70))
#main主标题、sub是副标题、xlab,ylab坐标轴名称
#xlim ylim是坐标轴范围

R基本图表| 散点图

4)Titles. -------> title and axis labels


        title(main="My Title", col.main="red",
              sub="My Sub-title", col.sub="blue",
              xlab="My X label", ylab="My Y label",
              col.lab="green", cex.lab=0.75)
#改了颜色设置和大小设置

5)axes---------->自己制作个性化坐标轴

axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...)

side: 在哪里画这根轴,数字表示上面还是下面还是左边还是右边

关于剩下的查书去吧,看的我累的慌

总之关于图新这一块怎么画好看的走在这一章,变量太多了,拿个好看的图自己研究去吧。