吴裕雄--天生自然 R语言开发学习:图形初阶(续一)
























# ----------------------------------------------------#
# R in Action (2nd ed): Chapter 3 #
# Getting started with graphs #
# requires that the Hmisc and RColorBrewer packages #
# have been installed #
# install.packages(c("Hmisc", "RColorBrewer")) #
#-----------------------------------------------------# par(ask=TRUE)
opar <- par(no.readonly=TRUE) # make a copy of current settings attach(mtcars) # be sure to execute this line plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
# Input data for drug example
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40) plot(dose, drugA, type="b") opar <- par(no.readonly=TRUE) # make a copy of current settings
par(lty=2, pch=17) # change line type and symbol
plot(dose, drugA, type="b") # generate a plot
par(opar) # restore the original settings plot(dose, drugA, type="b", lty=3, lwd=3, pch=15, cex=2) # choosing colors
library(RColorBrewer)
n <- 7
mycolors <- brewer.pal(n, "Set1")
barplot(rep(1,n), col=mycolors) n <- 10
mycolors <- rainbow(n)
pie(rep(1, n), labels=mycolors, col=mycolors)
mygrays <- gray(0:n/n)
pie(rep(1, n), labels=mygrays, col=mygrays) # Listing 3.1 - Using graphical parameters to control graph appearance
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly=TRUE)
par(pin=c(2, 3))
par(lwd=2, cex=1.5)
par(cex.axis=.75, font.axis=3)
plot(dose, drugA, type="b", pch=19, lty=2, col="red")
plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green")
par(opar) # Adding text, lines, and symbols
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)) # Listing 3.2 - An Example of Custom Axes
x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly=TRUE)
par(mar=c(5, 4, 4, 8) + 0.1)
plot(x, y, type="b",
pch=21, col="red",
yaxt="n", lty=3, ann=FALSE)
lines(x, z, type="b", pch=22, col="blue", lty=2)
axis(2, at=x, labels=x, col.axis="red", las=2)
axis(4, at=z, labels=round(z, digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
mtext("y=1/x", side=4, line=3, cex.lab=1, las=2, col="blue")
title("An Example of Creative Axes",
xlab="X values",
ylab="Y=X")
par(opar) # Listing 3.3 - Comparing Drug A and Drug B response by dose
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly=TRUE)
par(lwd=2, cex=1.5, font.lab=2)
plot(dose, drugA, type="b",
pch=15, lty=1, col="red", ylim=c(0, 60),
main="Drug A vs. Drug B",
xlab="Drug Dosage", ylab="Drug Response")
lines(dose, drugB, type="b",
pch=17, lty=2, col="blue")
abline(h=c(30), lwd=1.5, lty=2, col="gray")
library(Hmisc)
minor.tick(nx=3, ny=3, tick.ratio=0.5)
legend("topleft", inset=.05, title="Drug Type", c("A","B"),
lty=c(1, 2), pch=c(15, 17), col=c("red", "blue"))
par(opar) # Example of labeling points
attach(mtcars)
plot(wt, mpg,
main="Mileage vs. Car Weight",
xlab="Weight", ylab="Mileage",
pch=18, col="blue")
text(wt, mpg,
row.names(mtcars),
cex=0.6, pos=4, col="red")
detach(mtcars) # View font families
opar <- par(no.readonly=TRUE)
par(cex=1.5)
plot(1:7,1:7,type="n")
text(3,3,"Example of default text")
text(4,4,family="mono","Example of mono-spaced text")
text(5,5,family="serif","Example of serif text")
par(opar) # Combining graphs
attach(mtcars)
opar <- par(no.readonly=TRUE)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs. disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
par(opar)
detach(mtcars) attach(mtcars)
opar <- par(no.readonly=TRUE)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)
par(opar)
detach(mtcars) attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars) attach(mtcars)
layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE),
widths=c(3, 1), heights=c(1, 2))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars) # Listing 3.4 - Fine placement of figures in a graph
opar <- par(no.readonly=TRUE)
par(fig=c(0, 0.8, 0, 0.8))
plot(mtcars$mpg, mtcars$wt,
xlab="Miles Per Gallon",
ylab="Car Weight")
par(fig=c(0, 0.8, 0.55, 1), new=TRUE)
boxplot(mtcars$mpg, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65, 1, 0, 0.8), new=TRUE)
boxplot(mtcars$wt, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
par(opar)
吴裕雄--天生自然 R语言开发学习:图形初阶(续一)的更多相关文章
- 吴裕雄--天生自然 R语言开发学习:聚类分析(续一)
		
#-------------------------------------------------------# # R in Action (2nd ed): Chapter 16 # # Clu ...
 - 吴裕雄--天生自然 R语言开发学习:时间序列(续三)
		
#-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...
 - 吴裕雄--天生自然 R语言开发学习:时间序列(续二)
		
#-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...
 - 吴裕雄--天生自然 R语言开发学习:时间序列(续一)
		
#-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...
 - 吴裕雄--天生自然 R语言开发学习:方差分析(续二)
		
#-------------------------------------------------------------------# # R in Action (2nd ed): Chapte ...
 - 吴裕雄--天生自然 R语言开发学习:方差分析(续一)
		
#-------------------------------------------------------------------# # R in Action (2nd ed): Chapte ...
 - 吴裕雄--天生自然 R语言开发学习:回归(续四)
		
#------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...
 - 吴裕雄--天生自然 R语言开发学习:回归(续三)
		
#------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...
 - 吴裕雄--天生自然 R语言开发学习:回归(续二)
		
#------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...
 - 吴裕雄--天生自然 R语言开发学习:回归(续一)
		
#------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...
 
随机推荐
- 第22章 Makefile基础
			
一.自动处理头文件的依赖关系 在Makefile中插入如下代码: include $(sources:.c=.d) %.d: %.c set -e; rm -f $@; \ $(CC) -MM $(C ...
 - Channels(纪念一下卡我心态的一道题)
			
链接:https://ac.nowcoder.com/acm/contest/3947/C来源:牛客网 题目描述 Nancy喜欢学习,也喜欢看电视. 为了想了解她能看多长时间的节目,不妨假设节目从时刻 ...
 - java使用forEach填充字典值
			
// 填充字典值 Vector vector = vectorMapper.selectByPrimaryKey(id); VectorModel vectorModel = new VectorMo ...
 - 【图论算法】Dijstra&BFS
			
选择V-S中的点加入S时用了贪心思想,即求d[]中legth最小且未被标记(未加入加入S)的点. 一点都没优化的实现: import java.lang.reflect.Array; /** * Cr ...
 - 洛谷 P1258 小车问题
			
题目传送门 解题思路: 首先,每个人都要做一次车,而且两个人要同时到达,这样才能使总时间最短. 那么,我们设起点为A,终点为B,小车先带甲开到C点后甲下车走到B点,同时小车掉头与已经走到D点的乙相向而 ...
 - rabbitmq参考文档
			
英文文档:http://www.rabbitmq.com/getstarted.html 中文文档:http://rabbitmq.mr-ping.com/ rabbitmq重启,消费者恢复,解决消费 ...
 - Python笔记_第三篇_面向对象_6.继承(单继承和多继承)
			
1. 概念解释: 继承:有两个类:A类和B类.那么A类就拥有了B类中的属性和方法. * 例如:Object:是所有类的父亲,还可以成为基类或者超类(super()) * 继承者为子类,被继承者成为父类 ...
 - setTimeout()执行时序
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
 - RL78 定义常量变量在指定的地址方法
			
若想定义的常量地址在远端寻址,定义section段时 如定义MCU_INFOR段 则段名为MCU_INFOR_f 后缀需要添加f,近端寻址添加n. 程序中定义常量 需要使用#pragma 指 ...
 - Pytorch基础——使用 RNN 生成简单序列
			
一.介绍 内容 使用 RNN 进行序列预测 今天我们就从一个基本的使用 RNN 生成简单序列的例子中,来窥探神经网络生成符号序列的秘密. 我们首先让神经网络模型学习形如 0^n 1^n 形式的上下文无 ...