1. #---------------------------------------------------------------#
  2. # R in Action (2nd ed): Chapter 6 #
  3. # Basic graphs #
  4. # requires packages vcd, plotrix, sm, vioplot to be installed #
  5. # install.packages(c("vcd", "plotrix", "sm", "vioplot")) #
  6. #---------------------------------------------------------------#
  7.  
  8. par(ask=TRUE)
  9. opar <- par(no.readonly=TRUE) # save original parameter settings
  10.  
  11. library(vcd)
  12. counts <- table(Arthritis$Improved)
  13. counts
  14.  
  15. # Listing 6.1 - Simple bar plot
  16. # vertical barplot
  17. barplot(counts,
  18. main="Simple Bar Plot",
  19. xlab="Improvement", ylab="Frequency")
  20. # horizontal bar plot
  21. barplot(counts,
  22. main="Horizontal Bar Plot",
  23. xlab="Frequency", ylab="Improvement",
  24. horiz=TRUE)
  25.  
  26. # obtain 2-way frequency table
  27. library(vcd)
  28. counts <- table(Arthritis$Improved, Arthritis$Treatment)
  29. counts
  30.  
  31. # Listing 6.2 - Stacked and grouped bar plots
  32. # stacked barplot
  33. barplot(counts,
  34. main="Stacked Bar Plot",
  35. xlab="Treatment", ylab="Frequency",
  36. col=c("red", "yellow","green"),
  37. legend=rownames(counts))
  38.  
  39. # grouped barplot
  40. barplot(counts,
  41. main="Grouped Bar Plot",
  42. xlab="Treatment", ylab="Frequency",
  43. col=c("red", "yellow", "green"),
  44. legend=rownames(counts), beside=TRUE)
  45.  
  46. # Listing 6.3 - Bar plot for sorted mean values
  47. states <- data.frame(state.region, state.x77)
  48. means <- aggregate(states$Illiteracy, by=list(state.region), FUN=mean)
  49. means
  50.  
  51. means <- means[order(means$x),]
  52. means
  53.  
  54. barplot(means$x, names.arg=means$Group.1)
  55. title("Mean Illiteracy Rate")
  56.  
  57. # Listing 6.4 - Fitting labels in bar plots
  58. par(las=2) # set label text perpendicular to the axis
  59. par(mar=c(5,8,4,2)) # increase the y-axis margin
  60. counts <- table(Arthritis$Improved) # get the data for the bars
  61.  
  62. # produce the graph
  63. barplot(counts,
  64. main="Treatment Outcome", horiz=TRUE, cex.names=0.8,
  65. names.arg=c("No Improvement", "Some Improvement", "Marked Improvement")
  66. )
  67. par(opar)
  68.  
  69. # Spinograms
  70. library(vcd)
  71. attach(Arthritis)
  72. counts <- table(Treatment,Improved)
  73. spine(counts, main="Spinogram Example")
  74. detach(Arthritis)
  75.  
  76. # Listing 6.5 - Pie charts
  77. par(mfrow=c(2,2))
  78. slices <- c(10, 12,4, 16, 8)
  79. lbls <- c("US", "UK", "Australia", "Germany", "France")
  80.  
  81. pie(slices, labels = lbls,
  82. main="Simple Pie Chart")
  83.  
  84. pct <- round(slices/sum(slices)*100)
  85. lbls <- paste(lbls, pct)
  86. lbls <- paste(lbls,"%",sep="")
  87. pie(slices,labels = lbls, col=rainbow(length(lbls)),
  88. main="Pie Chart with Percentages")
  89.  
  90. library(plotrix)
  91. pie3D(slices, labels=lbls,explode=0.1,
  92. main="3D Pie Chart ")
  93.  
  94. mytable <- table(state.region)
  95. lbls <- paste(names(mytable), "\n", mytable, sep="")
  96. pie(mytable, labels = lbls,
  97. main="Pie Chart from a dataframe\n (with sample sizes)")
  98.  
  99. par(opar)
  100.  
  101. # Fan plots
  102. library(plotrix)
  103. slices <- c(10, 12,4, 16, 8)
  104. lbls <- c("US", "UK", "Australia", "Germany", "France")
  105. fan.plot(slices, labels = lbls, main="Fan Plot")
  106.  
  107. # Listing 6.6 - Histograms
  108. # simple histogram 1
  109. hist(mtcars$mpg)
  110.  
  111. # colored histogram with specified number of bins
  112. hist(mtcars$mpg,
  113. breaks=12,
  114. col="red",
  115. xlab="Miles Per Gallon",
  116. main="Colored histogram with 12 bins")
  117.  
  118. # colored histogram with rug plot, frame, and specified number of bins
  119. hist(mtcars$mpg,
  120. freq=FALSE,
  121. breaks=12,
  122. col="red",
  123. xlab="Miles Per Gallon",
  124. main="Histogram, rug plot, density curve")
  125. rug(jitter(mtcars$mpg))
  126. lines(density(mtcars$mpg), col="blue", lwd=2)
  127.  
  128. # histogram with superimposed normal curve (Thanks to Peter Dalgaard)
  129. x <- mtcars$mpg
  130. h<-hist(x,
  131. breaks=12,
  132. col="red",
  133. xlab="Miles Per Gallon",
  134. main="Histogram with normal curve and box")
  135. xfit<-seq(min(x),max(x),length=40)
  136. yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
  137. yfit <- yfit*diff(h$mids[1:2])*length(x)
  138. lines(xfit, yfit, col="blue", lwd=2)
  139. box()
  140.  
  141. # Listing 6.7 - Kernel density plot
  142. d <- density(mtcars$mpg) # returns the density data
  143. plot(d) # plots the results
  144.  
  145. d <- density(mtcars$mpg)
  146. plot(d, main="Kernel Density of Miles Per Gallon")
  147. polygon(d, col="red", border="blue")
  148. rug(mtcars$mpg, col="brown")
  149.  
  150. # Listing 6.8 - Comparing kernel density plots
  151. par(lwd=2)
  152. library(sm)
  153. attach(mtcars)
  154.  
  155. # create value labels
  156. cyl.f <- factor(cyl, levels= c(4, 6, 8),
  157. labels = c("4 cylinder", "6 cylinder", "8 cylinder"))
  158.  
  159. # plot densities
  160. sm.density.compare(mpg, cyl, xlab="Miles Per Gallon")
  161. title(main="MPG Distribution by Car Cylinders")
  162.  
  163. # add legend via mouse click
  164. colfill<-c(2:(2+length(levels(cyl.f))))
  165. cat("Use mouse to place legend...","\n\n")
  166. legend(locator(1), levels(cyl.f), fill=colfill)
  167. detach(mtcars)
  168. par(lwd=1)
  169.  
  170. # parallel box plots
  171. boxplot(mpg~cyl,data=mtcars,
  172. main="Car Milage Data",
  173. xlab="Number of Cylinders",
  174. ylab="Miles Per Gallon")
  175.  
  176. # notched box plots
  177. boxplot(mpg~cyl,data=mtcars,
  178. notch=TRUE,
  179. varwidth=TRUE,
  180. col="red",
  181. main="Car Mileage Data",
  182. xlab="Number of Cylinders",
  183. ylab="Miles Per Gallon")
  184.  
  185. # Listing 6.9 - Box plots for two crossed factors
  186. # create a factor for number of cylinders
  187. mtcars$cyl.f <- factor(mtcars$cyl,
  188. levels=c(4,6,8),
  189. labels=c("","",""))
  190.  
  191. # create a factor for transmission type
  192. mtcars$am.f <- factor(mtcars$am,
  193. levels=c(0,1),
  194. labels=c("auto","standard"))
  195.  
  196. # generate boxplot
  197. boxplot(mpg ~ am.f *cyl.f,
  198. data=mtcars,
  199. varwidth=TRUE,
  200. col=c("gold", "darkgreen"),
  201. main="MPG Distribution by Auto Type",
  202. xlab="Auto Type")
  203.  
  204. # Listing 6.10 - Violin plots
  205.  
  206. library(vioplot)
  207. x1 <- mtcars$mpg[mtcars$cyl==4]
  208. x2 <- mtcars$mpg[mtcars$cyl==6]
  209. x3 <- mtcars$mpg[mtcars$cyl==8]
  210. vioplot(x1, x2, x3,
  211. names=c("4 cyl", "6 cyl", "8 cyl"),
  212. col="gold")
  213. title("Violin Plots of Miles Per Gallon")
  214.  
  215. # dot chart
  216. dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7,
  217. main="Gas Mileage for Car Models",
  218. xlab="Miles Per Gallon")
  219.  
  220. # Listing 6.11 - Dot plot grouped, sorted, and colored
  221. x <- mtcars[order(mtcars$mpg),]
  222. x$cyl <- factor(x$cyl)
  223. x$color[x$cyl==4] <- "red"
  224. x$color[x$cyl==6] <- "blue"
  225. x$color[x$cyl==8] <- "darkgreen"
  226. dotchart(x$mpg,
  227. labels = row.names(x),
  228. cex=.7,
  229. pch=19,
  230. groups = x$cyl,
  231. gcolor = "black",
  232. color = x$color,
  233. main = "Gas Mileage for Car Models\ngrouped by cylinder",
  234. xlab = "Miles Per Gallon")

吴裕雄--天生自然 R语言开发学习:基本图形的更多相关文章

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

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

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

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

  3. 吴裕雄--天生自然 R语言开发学习:图形初阶

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

  4. 吴裕雄--天生自然 R语言开发学习:R语言的安装与配置

    下载R语言和开发工具RStudio安装包 先安装R

  5. 吴裕雄--天生自然 R语言开发学习:数据集和数据结构

    数据集的概念 数据集通常是由数据构成的一个矩形数组,行表示观测,列表示变量.表2-1提供了一个假想的病例数据集. 不同的行业对于数据集的行和列叫法不同.统计学家称它们为观测(observation)和 ...

  6. 吴裕雄--天生自然 R语言开发学习:导入数据

    2.3.6 导入 SPSS 数据 IBM SPSS数据集可以通过foreign包中的函数read.spss()导入到R中,也可以使用Hmisc 包中的spss.get()函数.函数spss.get() ...

  7. 吴裕雄--天生自然 R语言开发学习:使用键盘、带分隔符的文本文件输入数据

    R可从键盘.文本文件.Microsoft Excel和Access.流行的统计软件.特殊格 式的文件.多种关系型数据库管理系统.专业数据库.网站和在线服务中导入数据. 使用键盘了.有两种常见的方式:用 ...

  8. 吴裕雄--天生自然 R语言开发学习:R语言的简单介绍和使用

    假设我们正在研究生理发育问 题,并收集了10名婴儿在出生后一年内的月龄和体重数据(见表1-).我们感兴趣的是体重的分 布及体重和月龄的关系. 可以使用函数c()以向量的形式输入月龄和体重数据,此函 数 ...

  9. 吴裕雄--天生自然 R语言开发学习:基础知识

    1.基础数据结构 1.1 向量 # 创建向量a a <- c(1,2,3) print(a) 1.2 矩阵 #创建矩阵 mymat <- matrix(c(1:10), nrow=2, n ...

  10. 吴裕雄--天生自然 R语言开发学习:基本图形(续二)

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

随机推荐

  1. PAT Basic 1007 素数对猜想 (20) [数学问题-素数]

    题目 让我们定义 dn 为:dn = pn+1 – pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数."素数对猜想"认为"存在⽆穷多对 ...

  2. Java 10按钮设计(awt)

    /** * 2019年8月9日08:03:41 * 目的:利用Java设计10个按钮 * @author 张涛 * */ //导入awt包 import java.awt.*; import java ...

  3. 基于node的前后端分离初识

    久闻node的大名,先后也看过node的文档,但是,总是碍于没有挑起我的G点,所以实际codeing的例子都没有.最近,突然很有兴致,想把原有用页面ajax渲染的页面采用服务端node来渲染,研究了两 ...

  4. 优秀的github java项目

    转载:https://www.zhihu.com/question/24834285/answer/251369977 biezhi/blade:先推荐下自己的哈哈,一款轻量级.高性能.简洁优雅的MV ...

  5. 迅为iTOP-开发板-驱动-can和rfid配置

    在迅为开发板中,在 4412,4418 以及 6818 中,有的开发板默认配置 RFID,有的默认配 置 CAN 驱动(IMX6 默认都配置). 本文档介绍如何配置 CAN 和 RFID 的驱动. 截 ...

  6. Python 爬虫 爬取图片入门

    爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本. 用户看到的网页实质是由 HTML 代码构成的,爬 ...

  7. 四、Shell脚本高级编程实战第四部

    一.比较两个数的大小 #!/bin/shread -p "Pls input two num:" a b[ -z "$a" ] || [ -z "$b ...

  8. 理解python的可变参数

    以 str.format(*args,**kwargs) 为例. "type1:{},{},{},{}_type2:{a},{b},{c},{d}".format('a',2,*[ ...

  9. 解决ubuntu16.04启动时长时间陷入紫屏

    今天我的ubuntu系统进不去,一启动就陷入紫屏的死循环中,重装了两遍系统还是一样进不去,后来上网查找了各种解决办法,网上都说是显卡的问题,我也不懂什么意思.试了几种方法,终于解决了这个问题,在这里记 ...

  10. Angular开发者指南(三)数据绑定

    数据绑定 AngularJS应用程序中的数据绑定是模型和视图组件之间的数据的自动同步. AngularJS实现数据绑定的方式可以将模型视为应用程序中的单一来源. 视图是模型在任何时候的投影. 当模型更 ...