创建散点图的基础函数是plot(x, y),其中,x和y时数值型向量。依旧采用书上的例子:
attach(mtcars)
plot(wt, mpg,
main = 'Basic Scatter plot of MPG vs. Wweight',
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)
#添加 最佳拟合曲线的散点图

car包中的 scatterplot()函数增强了散点图的许多功能,并能添加拟合曲线、边界箱线图和置信椭圆,还可以按子集绘图和交互式地识别点。
install.packages('car')
library(car)
scatterplot(mpg~wt | cyl, data=mtcars, lwd=2,
main = 'Basic Scatter plot of MPG vs. Wweight by # Cylinders',
xlab = 'Weight of Car (lbs/1000)',
ylab = 'Miles Per Gallon',
legend=TRUE,
id='identify',
boxplots='xy')
# 若id=FALSE,则不显示点标签
这边书籍中原始代码有错误,此处建议使用 help(scatterplot)寻求帮助。
# 散点图矩阵 pairs()可以创建基础的散点图矩阵
pairs(~mpg+disp+drat+wt, data = mtcars,
main='Basic Scatter Plot Matrix')
scatterplotMatrix(~ mpg+disp+drat+wt, data=mtcars,
spread=FALSE, lty.smooth=2,
main='Scatter Plot Matrix via car Package')
# 上述代码中,线性和平滑(loess)拟合曲线被默认添加
scatterplotMatrix(
~ mpg + disp + drat + wt | cyl,
data = mtcars,
spread = FALSE,
diagonal = 'histogram',
main = 'Scatter Plot Matrix via car Package'
)
cor(mtcars[c('mpg', 'wt', 'disp', 'drat')])
很奇怪,为什么我的直方图没有显示出来,反而是多了几条曲线,这个暂时这样,因为日后用不到。
对散点图矩阵中的这些变量重新排序并添加颜色。
#gclus包生成的散点图矩阵(对变量重新排序并添加颜色)
install.packages('gclus')
library(gclus)
mydata <- mtcars[c(1,3,5,6)]
mydata.corr <- abs(cor(mydata))
mycolors <- dmat.color(mydata.corr)
myorder <- order.single(mydata.corr)
cpairs(mydata, myorder,panel.colors = mycolors,
gap=.5, main='Variables Ordered and Colored by Correlation')
# gclus包中的cpairs()函数生成的散点图矩阵。变量离主对角线越近,相关性越高
高密度散点图
# 高密度散点图
set.seed(1234)
n <- 10000
c1 <- matrix(rnorm(n, mean=0, sd=0.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 10,000 Observation'))
# smoothScatter()函数可利用核密度估计生成用颜色密度来表示点分布的散点图。
with(mydata,
smoothScatter(x, y, main='Scatterplot Colored by Smoothed Densities'))
# 更加直观方式
install.packages('hexbin')
library(hexbin)
with(mydata,{
bin <- hexbin(x,y, xbins=50)
plot(bin, main='Hexagonal Binning with 10,000 Observations')
})
# 通过颜色来展示点的密度
install.packages('IDPmisc')
library(IDPmisc)
with(mydata,
iplot(x,y, main='Image Scatter Plot with Color Indicating Density'))
三维散点图暂时没有学,花里花哨的,平时用起来真的比较少,可以说不可能会用。气泡图之后再补上,今天喝了点酒,不行了,我要先下线了。
------------------------------------------ 手动分割线 -----------------------------
好了,现在把三维散点图补上,其实还挺有意思的,特别是后面几个可以交互的,气泡图倒是真的用的不多,除了在商业中比较流行。
# 三维散点图
install.packages("scatterplot3d")
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='Basic 3D Scatter Plot')
# 添加一个回归面
s3d <- scatterplot3d(wt, disp, mpg,
pch=16, highlight.3d = TRUE,
type = 'h',
main='Basic 3D Scatter Plot')
fit <- lm(mpg ~ wt+disp)
s3d$plane3d(fit)
# 保存图片
jpeg(file = '三维散点图.jpeg',
width=600,height=600,
quality=100,unit='px')
# 自由旋转 (有趣)
install.packages('rgl')
library(rgl)
plot3d(wt, disp, mpg, col='red', size=5)
#另一种旋转三维散点图
install.packages('Rcmdr')
library(Rcmdr)
attach(mtcars)
scatter3d(wt, disp, mpg)
# 气泡图
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)
# 在统计学中使用R都避免使用气泡图,因为看不清

好了,下面要学习的是折线图,暂时先这样,因为今天还学了怎么保存数据,也一并在上续代码中加了,以后不用到处翻资料了。
本人的文档都是自我记录,以便日后查看。
浙公网安备 33010602011771号