1. # ----------------------------------------------------#
  2. # R in Action (2nd ed): Chapter 3 #
  3. # Getting started with graphs #
  4. # requires that the Hmisc and RColorBrewer packages #
  5. # have been installed #
  6. # install.packages(c("Hmisc", "RColorBrewer")) #
  7. #-----------------------------------------------------#
  8.  
  9. par(ask=TRUE)
  10. opar <- par(no.readonly=TRUE) # make a copy of current settings
  11.  
  12. attach(mtcars) # be sure to execute this line
  13.  
  14. plot(wt, mpg)
  15. abline(lm(mpg~wt))
  16. title("Regression of MPG on Weight")
  17. # Input data for drug example
  18. dose <- c(20, 30, 40, 45, 60)
  19. drugA <- c(16, 20, 27, 40, 60)
  20. drugB <- c(15, 18, 25, 31, 40)
  21.  
  22. plot(dose, drugA, type="b")
  23.  
  24. opar <- par(no.readonly=TRUE) # make a copy of current settings
  25. par(lty=2, pch=17) # change line type and symbol
  26. plot(dose, drugA, type="b") # generate a plot
  27. par(opar) # restore the original settings
  28.  
  29. plot(dose, drugA, type="b", lty=3, lwd=3, pch=15, cex=2)
  30.  
  31. # choosing colors
  32. library(RColorBrewer)
  33. n <- 7
  34. mycolors <- brewer.pal(n, "Set1")
  35. barplot(rep(1,n), col=mycolors)
  36.  
  37. n <- 10
  38. mycolors <- rainbow(n)
  39. pie(rep(1, n), labels=mycolors, col=mycolors)
  40. mygrays <- gray(0:n/n)
  41. pie(rep(1, n), labels=mygrays, col=mygrays)
  42.  
  43. # Listing 3.1 - Using graphical parameters to control graph appearance
  44. dose <- c(20, 30, 40, 45, 60)
  45. drugA <- c(16, 20, 27, 40, 60)
  46. drugB <- c(15, 18, 25, 31, 40)
  47. opar <- par(no.readonly=TRUE)
  48. par(pin=c(2, 3))
  49. par(lwd=2, cex=1.5)
  50. par(cex.axis=.75, font.axis=3)
  51. plot(dose, drugA, type="b", pch=19, lty=2, col="red")
  52. plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green")
  53. par(opar)
  54.  
  55. # Adding text, lines, and symbols
  56. plot(dose, drugA, type="b",
  57. col="red", lty=2, pch=2, lwd=2,
  58. main="Clinical Trials for Drug A",
  59. sub="This is hypothetical data",
  60. xlab="Dosage", ylab="Drug Response",
  61. xlim=c(0, 60), ylim=c(0, 70))
  62.  
  63. # Listing 3.2 - An Example of Custom Axes
  64. x <- c(1:10)
  65. y <- x
  66. z <- 10/x
  67. opar <- par(no.readonly=TRUE)
  68. par(mar=c(5, 4, 4, 8) + 0.1)
  69. plot(x, y, type="b",
  70. pch=21, col="red",
  71. yaxt="n", lty=3, ann=FALSE)
  72. lines(x, z, type="b", pch=22, col="blue", lty=2)
  73. axis(2, at=x, labels=x, col.axis="red", las=2)
  74. axis(4, at=z, labels=round(z, digits=2),
  75. col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
  76. mtext("y=1/x", side=4, line=3, cex.lab=1, las=2, col="blue")
  77. title("An Example of Creative Axes",
  78. xlab="X values",
  79. ylab="Y=X")
  80. par(opar)
  81.  
  82. # Listing 3.3 - Comparing Drug A and Drug B response by dose
  83. dose <- c(20, 30, 40, 45, 60)
  84. drugA <- c(16, 20, 27, 40, 60)
  85. drugB <- c(15, 18, 25, 31, 40)
  86. opar <- par(no.readonly=TRUE)
  87. par(lwd=2, cex=1.5, font.lab=2)
  88. plot(dose, drugA, type="b",
  89. pch=15, lty=1, col="red", ylim=c(0, 60),
  90. main="Drug A vs. Drug B",
  91. xlab="Drug Dosage", ylab="Drug Response")
  92. lines(dose, drugB, type="b",
  93. pch=17, lty=2, col="blue")
  94. abline(h=c(30), lwd=1.5, lty=2, col="gray")
  95. library(Hmisc)
  96. minor.tick(nx=3, ny=3, tick.ratio=0.5)
  97. legend("topleft", inset=.05, title="Drug Type", c("A","B"),
  98. lty=c(1, 2), pch=c(15, 17), col=c("red", "blue"))
  99. par(opar)
  100.  
  101. # Example of labeling points
  102. attach(mtcars)
  103. plot(wt, mpg,
  104. main="Mileage vs. Car Weight",
  105. xlab="Weight", ylab="Mileage",
  106. pch=18, col="blue")
  107. text(wt, mpg,
  108. row.names(mtcars),
  109. cex=0.6, pos=4, col="red")
  110. detach(mtcars)
  111.  
  112. # View font families
  113. opar <- par(no.readonly=TRUE)
  114. par(cex=1.5)
  115. plot(1:7,1:7,type="n")
  116. text(3,3,"Example of default text")
  117. text(4,4,family="mono","Example of mono-spaced text")
  118. text(5,5,family="serif","Example of serif text")
  119. par(opar)
  120.  
  121. # Combining graphs
  122. attach(mtcars)
  123. opar <- par(no.readonly=TRUE)
  124. par(mfrow=c(2,2))
  125. plot(wt,mpg, main="Scatterplot of wt vs. mpg")
  126. plot(wt,disp, main="Scatterplot of wt vs. disp")
  127. hist(wt, main="Histogram of wt")
  128. boxplot(wt, main="Boxplot of wt")
  129. par(opar)
  130. detach(mtcars)
  131.  
  132. attach(mtcars)
  133. opar <- par(no.readonly=TRUE)
  134. par(mfrow=c(3,1))
  135. hist(wt)
  136. hist(mpg)
  137. hist(disp)
  138. par(opar)
  139. detach(mtcars)
  140.  
  141. attach(mtcars)
  142. layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
  143. hist(wt)
  144. hist(mpg)
  145. hist(disp)
  146. detach(mtcars)
  147.  
  148. attach(mtcars)
  149. layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE),
  150. widths=c(3, 1), heights=c(1, 2))
  151. hist(wt)
  152. hist(mpg)
  153. hist(disp)
  154. detach(mtcars)
  155.  
  156. # Listing 3.4 - Fine placement of figures in a graph
  157. opar <- par(no.readonly=TRUE)
  158. par(fig=c(0, 0.8, 0, 0.8))
  159. plot(mtcars$mpg, mtcars$wt,
  160. xlab="Miles Per Gallon",
  161. ylab="Car Weight")
  162. par(fig=c(0, 0.8, 0.55, 1), new=TRUE)
  163. boxplot(mtcars$mpg, horizontal=TRUE, axes=FALSE)
  164. par(fig=c(0.65, 1, 0, 0.8), new=TRUE)
  165. boxplot(mtcars$wt, axes=FALSE)
  166. mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
  167. par(opar)

吴裕雄--天生自然 R语言开发学习:图形初阶(续二)的更多相关文章

  1. 吴裕雄--天生自然 R语言开发学习:时间序列(续二)

    #-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...

  2. 吴裕雄--天生自然 R语言开发学习:方差分析(续二)

    #-------------------------------------------------------------------# # R in Action (2nd ed): Chapte ...

  3. 吴裕雄--天生自然 R语言开发学习:回归(续二)

    #------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...

  4. 吴裕雄--天生自然 R语言开发学习:分类(续二)

    #-----------------------------------------------------------------------------# # R in Action (2nd e ...

  5. 吴裕雄--天生自然 R语言开发学习:聚类分析(续一)

    #-------------------------------------------------------# # R in Action (2nd ed): Chapter 16 # # Clu ...

  6. 吴裕雄--天生自然 R语言开发学习:时间序列(续三)

    #-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...

  7. 吴裕雄--天生自然 R语言开发学习:时间序列(续一)

    #-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...

  8. 吴裕雄--天生自然 R语言开发学习:方差分析(续一)

    #-------------------------------------------------------------------# # R in Action (2nd ed): Chapte ...

  9. 吴裕雄--天生自然 R语言开发学习:回归(续四)

    #------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...

  10. 吴裕雄--天生自然 R语言开发学习:回归(续三)

    #------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...

随机推荐

  1. UML-从需求到设计--迭代进化

    按照UP原则,初始阶段做10%的需求,而细化阶段开始为这10%的需求设计解决方案.后续章节介绍如何设计.

  2. 题解 P5837 【[USACO19DEC]Milk Pumping】

    这题其实想法挺简单的,因为他只需要简单的把每个点的花费和流量用dp记下来就好了 1.怎么记: 首先考虑dp的状态.由于所在的点和流量都要记,所以dp开二维,一维记所在的点,另一维记去哪 //dp[i] ...

  3. Python—使用列表构造栈数据结构

    class Stack(object): """ 使用列表实现栈 """ def __init__(self): self.stack = ...

  4. ZJNU 1535 - 新建的大楼--中高级

    因为从俯视图看,输入输出的视角是从右下方看向左上方的 所以左上角的正方体最有可能被其他正方体挡住 立体上,底部的正方体最有可能被顶部的正方体挡住 所以绘图应该从后往前,从下往上绘制 剩下的就是一大堆计 ...

  5. MySQL--事务控制和锁定语句

    MySQL 支持对 MyISAM 和 MEMORY 存储引擎的表进行表级锁定,对 BDB 存储引擎的表进行页级锁定,对 InnoDB 存储引擎的表进行行级锁定.默认情况下,表锁和行锁都是自动获得的,不 ...

  6. js等于符号的详解

    JavaScript == 与 === 区别 1.对于 string.number 等基础类型,== 和 === 是有区别的 a)不同类型间比较,== 之比较 "转化成同一类型后的值&quo ...

  7. ruoyi IpUtils

    package com.ruoyi.common.utils; import java.net.InetAddress; import java.net.UnknownHostException; i ...

  8. 通过TleChat插件一键Getshell

    TleChat网站插件是一个发布到wordpress,typecho和emlog社区上的站长聊天插件,站长聊天室插件为站长和用户提供聊天室功能,让站长与用户之间的联系更加友爱,支持文本.长文本.语音聊 ...

  9. 洛谷-P3796-【模板】AC自动机(加强版)

    题目传送门 -------------------------------------- 过年在家无聊补一下这周做的几道AC自动机的模板题 sol:AC自动机,在fail边的基础上再加一个last边, ...

  10. Matlab高级教程_第三篇:Matlab转码C/C++方式(混编)_第二部分

    这一部分通过一些实例来进行转码和调试的讲解: 1. 输入变量.输出变量和过程内变量的内存预分配 函数代码:函数名test function [A,B] = test( mark,num,array ) ...