par(ask=TRUE)
opar <- par(no.readonly=TRUE) # record current settings # Listing 11.1 - A scatter plot with best fit lines
attach(mtcars)
plot(wt, mpg,
main="Basic Scatterplot of MPG vs. Weight",
xlab="Car Weight (lbs/1000)",
ylab="Miles Per Gallon ", pch=19)
abline(lm(mpg ~ wt), col="red", lwd=2, lty=1)
lines(lowess(wt, mpg), col="blue", lwd=2, lty=2)
detach(mtcars)

# Scatter plot with fit lines by group
library(car)
scatterplot(mpg ~ wt | cyl, data=mtcars, lwd=2,main="Scatter Plot of MPG vs. Weight by # Cylinders", xlab="Weight of Car (lbs/1000)",ylab="Miles Per Gallon",id.method="identify",legend.plot=TRUE,boxplots="xy")

# Scatter-plot matrices
pairs(~ mpg + disp + drat + wt, data=mtcars,
main="Basic Scatterplot Matrix")

library(car)
scatterplotMatrix(~ mpg + disp + drat + wt, data=mtcars,
spread=FALSE, smoother.args=list(lty=2),
main="Scatter Plot Matrix via car Package")

# high density scatterplots
set.seed(1234)
n <- 10000
c1 <- matrix(rnorm(n, mean=0, sd=.5), ncol=2)
c2 <- matrix(rnorm(n, mean=3, sd=2), ncol=2)
mydata <- rbind(c1, c2)
mydata <- as.data.frame(mydata)
names(mydata) <- c("x", "y") with(mydata,
plot(x, y, pch=19, main="Scatter Plot with 10000 Observations")) with(mydata,
smoothScatter(x, y, main="Scatter Plot colored by Smoothed Densities")) library(hexbin)
with(mydata, {
bin <- hexbin(x, y, xbins=50)
plot(bin, main="Hexagonal Binning with 10,000 Observations")
})

# 3-D Scatterplots
library(scatterplot3d)
attach(mtcars)
scatterplot3d(wt, disp, mpg,
main="Basic 3D Scatter Plot") scatterplot3d(wt, disp, mpg,
pch=16,
highlight.3d=TRUE,
type="h",
main="3D Scatter Plot with Vertical Lines") s3d <-scatterplot3d(wt, disp, mpg,
pch=16,
highlight.3d=TRUE,
type="h",
main="3D Scatter Plot with Vertical Lines and Regression Plane")
fit <- lm(mpg ~ wt+disp)
s3d$plane3d(fit)
detach(mtcars)

# spinning 3D plot
library(rgl)
attach(mtcars)
plot3d(wt, disp, mpg, col="red", size=5)

# alternative
library(car)
with(mtcars,
scatter3d(wt, disp, mpg))

# bubble plots
attach(mtcars)
r <- sqrt(disp/pi)
symbols(wt, mpg, circle=r, inches=0.30,
fg="white", bg="lightblue",
main="Bubble Plot with point size proportional to displacement",
ylab="Miles Per Gallon",
xlab="Weight of Car (lbs/1000)")
text(wt, mpg, rownames(mtcars), cex=0.6)
detach(mtcars)

# Listing 11.2 - Creating side by side scatter and line plots
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,2))
t1 <- subset(Orange, Tree==1) plot(t1$age, t1$circumference,
xlab="Age (days)",
ylab="Circumference (mm)",
main="Orange Tree 1 Growth") plot(t1$age, t1$circumference,
xlab="Age (days)",
ylab="Circumference (mm)",
main="Orange Tree 1 Growth",
type="b") par(opar)

# Listing 11.3 - Line chart displaying the growth of 5 Orange trees over time
Orange$Tree <- as.numeric(Orange$Tree)
ntrees <- max(Orange$Tree)
xrange <- range(Orange$age)
yrange <- range(Orange$circumference)
plot(xrange, yrange,
type="n",
xlab="Age (days)",
ylab="Circumference (mm)"
) colors <- rainbow(ntrees)
linetype <- c(1:ntrees)
plotchar <- seq(18, 18+ntrees, 1)
for (i in 1:ntrees) {
tree <- subset(Orange, Tree==i)
lines(tree$age, tree$circumference,
type="b",
lwd=2,
lty=linetype[i],
col=colors[i],
pch=plotchar[i]
)
}
title("Tree Growth", "example of line plot")
legend(xrange[1], yrange[2],
1:ntrees,
cex=0.8,
col=colors,
pch=plotchar,
lty=linetype,
title="Tree"
)

# Correlograms
options(digits=2)
cor(mtcars) library(corrgram)
corrgram(mtcars, order=TRUE, lower.panel=panel.shade,
upper.panel=panel.pie, text.panel=panel.txt,
main="Corrgram of mtcars intercorrelations") corrgram(mtcars, order=TRUE, lower.panel=panel.ellipse,
upper.panel=panel.pts, text.panel=panel.txt,
diag.panel=panel.minmax,
main="Corrgram of mtcars data using scatter plots
and ellipses") cols <- colorRampPalette(c("darkgoldenrod4", "burlywood1",
"darkkhaki", "darkgreen"))
corrgram(mtcars, order=TRUE, col.regions=cols,
lower.panel=panel.shade,
upper.panel=panel.conf, text.panel=panel.txt,
main="A Corrgram (or Horse) of a Different Color")

# Mosaic Plots
ftable(Titanic)
library(vcd)
mosaic(Titanic, shade=TRUE, legend=TRUE) library(vcd)
mosaic(~Class+Sex+Age+Survived, data=Titanic, shade=TRUE, legend=TRUE)

# type= options in the plot() and lines() functions
x <- c(1:5)
y <- c(1:5)
par(mfrow=c(2,4))
types <- c("p", "l", "o", "b", "c", "s", "S", "h")
for (i in types){
plottitle <- paste("type=", i)
plot(x,y,type=i, col="red", lwd=2, cex=1, main=plottitle)
}

吴裕雄--天生自然 R语言数据可视化绘图(3)的更多相关文章

  1. 吴裕雄--天生自然 R语言数据可视化绘图(4)

    par(ask=TRUE) # Basic scatterplot library(ggplot2) ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_poin ...

  2. 吴裕雄--天生自然 R语言数据可视化绘图(2)

    par(ask=TRUE) opar <- par(no.readonly=TRUE) # save original parameter settings library(vcd) count ...

  3. 吴裕雄--天生自然 R语言数据可视化绘图(1)

    par(ask=TRUE) opar <- par(no.readonly=TRUE) # make a copy of current settings attach(mtcars) # be ...

  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 in Action (2nd ed): Chapter 18 # # Advanced methods for mi ...

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

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

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

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

随机推荐

  1. SpringCloud与微服务Ⅷ --- Hystrix断路器

    复杂的分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败. 服务雪崩 多个微服务之间调用的时候,假设微服务调用服务B和微服务C,微服务B和微服务C又调用其他服务,这就是 ...

  2. 浏览器无法进入GitHub(已解决)

    时间:2020/1/22 今天突然chrome登不上GitHub,一直出现响应时间过长的问题,如下: 开始还以为是GitHub服务器出问题了(虽然概率很小.....),但这种情况一直持续了几个小时,我 ...

  3. 用上自己的线程池,实现自己的RPC框架

    package github.com.AllenDuke.rpc.customer; import github.com.AllenDuke.rpc.netty.NettyClient; import ...

  4. PAT_B_PRAC_1003养兔子

    题目描述 一只成熟的兔子每天能产下一胎兔子.每只小兔子的成熟期是一天. 某人领养了一只小兔子,请问第N天以后,他将会得到多少只兔子. 输入描述: 测试数据包括多组,每组一行,为整数n(1≤n≤90). ...

  5. caffe 指定GPU

    caffe默认使用编号为0的gpu, 若它的内存不够或正忙, 即使有其余gpu空闲, caffe也不会使用. 要用哪个gpu, 就要明确指定哪个. 不指定则使用默认. 命令行 ./build/tool ...

  6. 机器学习总结-bias–variance tradeoff

    bias–variance tradeoff 通过机器学习,我们可以从历史数据学到一个\(f\),使得对新的数据\(x\),可以利用学到的\(f\)得到输出值\(f(x)\).设我们不知道的真实的\( ...

  7. (二)MyBatis延迟加载,一级缓存,二级缓存

    延迟加载配置: 什么时候用延迟加载?比如现在有班级和学生表,一对多关系,你可能只需要班级的信息,而不需要该班级学生的信息,这时候可以进行配置,让查询时先查询到班级的信息,在之后需要学生信息时候,再进行 ...

  8. 数据结构与算法的实现(c++)之第一天

    开发工具:codeblocks 17.12版本 学习视频来自b站 第一天:学习swap交换.冒泡排序 swap交换:swap是几乎所有的排序的最基础部分,代码如下: #include <iost ...

  9. Gorm与数据库(单复数)表结构之间的映射

    Gorm连接MySQL: import ( _ "github.com/go-sql-driver/mysql" "github.com/jinzhu/gorm" ...

  10. 从linux命令行分享文件:bashupload.com和transfer.sh

    背景 传输文件是一个常见的需求,简单的做法是通过即时通讯工具,邮件,网盘完成. 但当分享或接收的一端为远程服务器,只有命令行可以操作时,一个能支持在命令行完成分享和下载的工具,就会省下不少麻烦. 下面 ...