当前位置:天才代写 > tutorial > R语言教程 > R语言教程之如何生成散点图Scatterplots

R语言教程之如何生成散点图Scatterplots

2018-05-09 08:00 星期三 所属: R语言教程 浏览:1612

简单的散点图

许多方法可以在R中创建散点图。基本函数是plot(,其中xy是表示要绘制的(x,y)点的数字向量。

# Simple Scatterplot
attach(mtcars)
plot(wt, mpg, main="Scatterplot Example", 
   xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)

简单的散点图 点击查看

(要练习制作一个简单的散点图,请尝试DataCamp中的这个交互式示例。

# Add fit lines
abline(lm(mpg~wt), col="red") # regression line (y~x) 
lines(lowess(wt,mpg), col="blue") # lowess line (x,y)

与适合的散点图 点击查看

汽车包装中scatterplot()函数提供了许多增强功能,包括拟合线,边缘箱形图,调节因素和交互点识别。每个功能都是可选的。

# Enhanced Scatterplot of MPG vs. Weight 
# by Number of Car Cylinders 
library(car) 
scatterplot(mpg ~ wt | cyl, data=mtcars, 
   xlab="Weight of Car", ylab="Miles Per Gallon", 
   main="Enhanced Scatter Plot", 
   labels=row.names(mtcars))

增强散点图 点击查看

散点图矩阵

创建散点图矩阵至少有4个有用的函数。分析师必须喜欢散点图矩阵!

# Basic Scatterplot Matrix
pairs(~mpg+disp+drat+wt,data=mtcars, 
   main="Simple Scatterplot Matrix")

简单的散点图矩阵 点击查看

晶格包提供选项,以调节上的因子散点图矩阵。

# Scatterplot Matrices from the lattice Package 
library(lattice)
splom(mtcars[c(1,3,5,6)], groups=cyl, data=mtcars,
   panel=panel.superpose, 
   key=list(title="Three Cylinder Options",
   columns=3,
   points=list(pch=super.sym$pch[1:3],
   col=super.sym$col[1:3]),
   text=list(c("4 Cylinder","6 Cylinder","8 Cylinder"))))

格子散点图矩阵 点击查看

汽车包可以调节上一个因子散点图矩阵,并且任选地包括LOWESS和线性最佳拟合线,和箱线图,密度,或直方图在主对角线,以及地毯地块在细胞中的边缘。

# Scatterplot Matrices from the car Package
library(car)
scatterplot.matrix(~mpg+disp+drat+wt|cyl, data=mtcars,
   main="Three Cylinder Options")

汽车散点图矩阵 点击查看

所述gclus包提供选项来重新排列变量,使得那些具有较高的相关性更接近主对角线。它还可以对单元格进行颜色编码以反映相关性的大小。

# Scatterplot Matrices from the glus Package 
library(gclus)
dta <- mtcars[c(1,3,5,6)] # get data 
dta.r <- abs(cor(dta)) # get correlations
dta.col <- dmat.color(dta.r) # get colors
# reorder variables so those with highest correlation
# are closest to the diagonal
dta.o <- order.single(dta.r) 
cpairs(dta, dta.o, panel.colors=dta.col, gap=.5,
main="Variables Ordered and Colored by Correlation" )

gclus散点图矩阵 点击查看

高密度散点图

当有许多数据点和显着的重叠时,散点图变得不那么有用。发生这种情况时有几种方法可以使用。Hexbin软件包中hexbin(x,y)函数为六角形单元格提供了二元binning(它看起来比听起来好)。

# High Density Scatterplot with Binning
library(hexbin)
x <- rnorm(1000)
y <- rnorm(1000)
bin<-hexbin(x, y, xbins=50) 
plot(bin, main="Hexagonal Binning")

六边形分档 点击查看

向日葵图的另一种选择是具有明显点重叠的散点图有关详细信息,请参阅帮助(sunflowerplot)

最后,您可以将散点图保存为PDF格式,并使用颜色透明度以允许重叠显示的点(该想法来自HSAUR中的BS Everrit)。

# High Density Scatterplot with Color Transparency 
pdf("c:/scatterplot.pdf") 
x <- rnorm(1000)
y <- rnorm(1000) 
plot(x,y, main="PDF Scatterplot Example", col=rgb(0,100,0,50,maxColorValue=255), pch=16)
dev.off()

散点图与阿尔法透明度 点击查看

注意:您可以使用col2rgb()函数来获取R颜色的rbg值。例如,col2rgb( “深绿色”)yeilds R = 0,G = 100,B = 0。然后将Alpha透明度级别添加为颜色矢量中的第4个数字。值为零意味着完全透明。请参阅帮助(rgb)了解更多信息。

3D散点图

您可以使用scatterplot3d软件包创建3D散点图使用函数scatterplot3d(  z)。

# 3D Scatterplot
library(scatterplot3d)
attach(mtcars)
scatterplot3d(wt,disp,mpg, main="3D Scatterplot")

3D散点图 点击查看

# 3D Scatterplot with Coloring and Vertical Drop Lines
library(scatterplot3d) 
attach(mtcars) 
scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,
  type="h", main="3D Scatterplot")

带有下拉线的3D散点图 点击查看

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

带回归平面的3D散点图 点击查看

旋转3D散点图

您还可以使用rgl包中plot3D(函数创建交互式3D散点图它创建一个旋转的3D散点图,可以用鼠标旋转。前三个参数是代表点x,y和z数字向量。col =和size =分别控制点的颜色和大小。

# Spinning 3d Scatterplot
library(rgl)

plot3d(wt, disp, mpg, col="red", size=3)

3D散点图 点击查看

您可以使用Rcmdr软件包中scatter3d(执行类似的功能

# Another Spinning 3d Scatterplot
library(Rcmdr)
attach(mtcars)
scatter3d(wt, disp, mpg)

 Rcmdr旋转3d scatterplot 点击查看

来练习

本课程中尝试创建散点图练习,以便在R中进行数据可视化。

 

    关键字:

天才代写-代写联系方式