注:R语言的再复习之路
1. 散点图
attach
(mtcars
)
plot
(wt
, mpg
)
abline
(lm
(mpg
~ wt
), col
= 'red', lwd
= 2, lty
= 1)
lines
(lowess
(wt
, mpg
), col
= 'blue', lwd
= 2, lty
= 2)
library
(car
)
scatterplot
(mpg
~ wt
| cyl
, data
= mtcars
, lwd
= 2, boxplots
= 'xy')
1.1 散点图矩阵
pairs
(~ mpg
+ disp
+ drat
+ wt
, data
= mtcars
)
library
(car
)
scatterplotMatrix
(~ mpg
+ disp
+ drat
+ wt
, data
= mtcars
)
library
(TeachingDemos
)
pairs2
(mtcars
[, c
(1, 3, 4)], mtcars
[, c
(5, 6, 7)], panel
= panel.smooth
)
library
(HH
)
xysplom
(mpg
+ disp
~ wt
+ drat
| cyl
, data
= mtcars
, layout
= c
(3, 4))
library
(ResourceSelection
)
kdepairs
(mtcars
[, c
(1, 3, 5)])
1.2 高密度散点图
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
, smoothScatter
(x
, y
, main
= 'Scatter Plot Colored by Smoothed Densities'))
library
(hexbin
)
with
(mydata
, {bin
<- hexbin
(x
, y
, xbins
= 50)
plot
(bin
)
})
1.3 三维散点图
library
(scatterplot3d
)
attach
(mtcars
)
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
)
1.4 旋转三维散点图
library
(rgl
)
attach
(mtcars
)
plot3d
(wt
, disp
, mpg
, col
= 'red', size
= 5)
library
(car
)
with
(mtcars
, scatter3d
(wt
, disp
, mpg
))
1.5 气泡图
attach
(mtcars
)
r
<- sqrt
(disp
/pi
)
symbols
(wt
, mpg
, circle
= r
, inches
= .30, fg
= 'white', bg
= 'lightblue')
text
(wt
, mpg
, rownames
(mtcars
), cex
= .6)
attach
(mtcars
)
2. 折线图
类型图形外观
p只有点l只有线o实心点和线(点覆盖在线上)b, c线连接点(c时不绘制点)s, S阶梯线h直方图式的垂直线n不生成任何点和线
如果对图形有要求,你可以先通过plot()函数中的type = 'n'选项来创建坐标轴、标题和其他图形特征,然后再使用lines()函数添加各种需要绘制的曲线
Orange
$Tree
<- as.numeric
(Orange
$Tree
)
ntrees
<- max
(Orange
$Tree
)
xrange
<- range
(Orange
$age
)
yrange
<- range
(Orange
$age
)
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
= .8, col
= colors
, pch
= plotchar
, lty
=linetype
, title
= 'Tree')
3. 相关图
library
(corrgram
)
corrgram
(mtcars
, order
= TRUE, lower.panel
= panel.shade
, upper.panel
= panel.pie
, text.panel
= panel.txt
, main
= 'Corrgram of mtcars intercorrelations')
位置面板选项描述
非对角线panel.pie用饼图的填充比例表示相关性大小非对角线panel.shade用阴影的深度来表示相关性大小非对角线panel.ellipse画一个置信椭圆和平滑曲线非对角线panel.pts画一个散点图非对角线panel.conf画出相关性及置信区间主对角线panel.txt输出变量名主对角线panel.minmax输出变量的最大值最小值和变量名主对角线panel.ednsity输出核密度曲线和变量名
4. 马赛克图
mosaic
(formula
, data
= )
library
(vcd
)
mosaic
(Titanic
, shade
= TRUE, legend
= TRUE)
mosaic
(~ Class
+ Sex
+ Age
+ Survived
, data
= Titanic
, shade
= TRUE, legend
= TRUE)
转载请注明原文地址: https://lol.8miu.com/read-17691.html