博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
吴裕雄--天生自然 R语言数据可视化绘图(1)
阅读量:5266 次
发布时间:2019-06-14

本文共 4007 字,大约阅读时间需要 13 分钟。

par(ask=TRUE)opar <- par(no.readonly=TRUE) # make a copy of current settingsattach(mtcars) # be sure to execute this lineplot(wt, mpg)abline(lm(mpg~wt))title("Regression of MPG on Weight")

# Input data for drug exampledose  <- 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 settingspar(lty=2, pch=17)            # change line type and symbolplot(dose, drugA, type="b")   # generate a plotpar(opar)                     # restore the original settings plot(dose, drugA, type="b", lty=3, lwd=3, pch=15, cex=2)

# choosing colorslibrary(RColorBrewer)n <- 7mycolors <- brewer.pal(n, "Set1")barplot(rep(1,n), col=mycolors)n <- 10mycolors <- rainbow(n)pie(rep(1, n), labels=mycolors, col=mycolors)mygrays <- gray(0:n/n)pie(rep(1, n), labels=mygrays, col=mygrays)

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 symbolsplot(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))

x <- c(1:10)y <- xz <- 10/xopar <- 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)

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)

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 graphsattach(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 graphopar <- 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)

 

转载于:https://www.cnblogs.com/tszr/p/11200097.html

你可能感兴趣的文章
go:channel(未完)
查看>>
[JS]递归对象或数组
查看>>
LeetCode(17) - Letter Combinations of a Phone Number
查看>>
Linux查找命令对比(find、locate、whereis、which、type、grep)
查看>>
路由器外接硬盘做nas可行吗?
查看>>
python:从迭代器,到生成器,再到协程的示例代码
查看>>
Java多线程系列——原子类的实现(CAS算法)
查看>>
多线程《三》进程与线程的区别
查看>>
linux sed命令
查看>>
html标签的嵌套规则
查看>>
[Source] Machine Learning Gathering/Surveys
查看>>
HTML <select> 标签
查看>>
类加载机制
查看>>
tju 1782. The jackpot
查看>>
湖南多校对抗赛(2015.03.28) H SG Value
查看>>
hdu1255扫描线计算覆盖两次面积
查看>>
hdu1565 用搜索代替枚举找可能状态或者轮廓线解(较优),参考poj2411
查看>>
bzoj3224 splay板子
查看>>
程序存储问题
查看>>
Mac版OBS设置详解
查看>>