四、R的数据可视化
基础绘图包---lattice包---ggplot2包---其他图形
1、具体目标~探索型~接受型2、数据和受众的特点~哪些变量最重要~受众的角色~合适的映射方式
映射的图形元素# 1、坐标位置# 2、线条# 3、尺寸# 4、色彩# 5、形状# 6、文字
数据可视化元素graphics(自带绘图包 plot)
plot(cars$dist~cars$speed) #点图plot(cars$dist,type = 'l') #折线图plot(cars$dist,type='h') #线直方图hist(cars$dist) #直方图
lattice包
library(lattice)num <- sample(1:3,size=50,replace=T) #1-3随机生成50次 有放回barchart(table(num)) #barchart 柱形图
qqmath(rnorm(100)) #qqmath 查看数据是否符合正态分布
关于分组stripplot(~Sepal.Length|Species,data = iris,layout=c(1,3))#长度 和种类分布;点图
densityplot(~Sepal.Length,groups = Species,data=iris,plot.points=F)
densityplot(~Sepal.Length|Species,data=iris,plot.points=F,layout=c(1,3))
bwplot 箱线图bwplot(Species~Sepal.Length,data = iris)
#两个变量之间的关系xyplot(Sepal.Width~Sepal.Length,groups = Species,data=iris)
#splom两个变量之间的关系splom(iris[1:4])
histogram(~Sepal.Length | Species,data=iris,layout=c(1,3))
三维图形library(plyr)func3d <- function(x,y){sin(x^2/2 - y^2/4)*cos(2*x - exp(y))}vec1 <- vec2 <- seq(0,2,length=0)para <- expand.grid(x=vec1,y=vec2)result6 <- mdply(.data = para,.fun = func3d)
wireframe(V1~x*y,data=result6,scales = list(arrows = FALSE),drape = TRUE, colorkey = F)
ggplot2包Data和Mapping以及几何对象Geom
library(ggplot2)p <- ggplot(data = mpg, #内置mpg数据mapping = aes(x=cty,y=hwy)) + geom_point()print(p)
geom_point() 绘制散点图#基础修改p <- ggplot(data = mpg, #内置mpg数据mapping = aes(x=cty,y=hwy,color=factor(year)))p <- p + geom_point() #绘制散点图p
增加统计变换Statp <- ggplot(data = mpg, #内置mpg数据mapping = aes(x=cty,y=hwy,color=factor(year)))p <- p + stat_smooth() #统计变换 做光滑曲线p
在图层layer中增加点和平滑线p <- ggplot(data = mpg,mapping = aes(x=cty,y=hwy))+geom_point(aes(color=factor(year)))+stat_smooth()
p <- ggplot(data = mpg,mapping = aes(x=cty,y=hwy,color=factor(year)))+geom_point()+stat_smooth()
标度Scale控制p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy)) +geom_point(aes(colour=factor(year))) +stat_smooth()+scale_color_manual(values=c('blue2','red4')) #标度控制
分面face控制p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy)) +geom_point(aes(colour=factor(year))) +stat_smooth()+scale_color_manual(values=c('blue2','red4'))+facet_wrap(~ year,ncol=1)
精雕细琢p <- ggplot(data=mpg, mapping=aes(x=cty,y=hwy))+geom_point(aes(colour=class,size=displ),alpha=0.5,position = "jitter")+stat_smooth()+scale_size_continuous(range = c(4, 10))+ #大小变化的范围facet_wrap(~ year,ncol=1)+ #labs(title='汽车型号与油耗',y='每加仑高速公路行驶距离',x='每加仑城市公路行驶距离',size='排量',colour = '车型')
直方图library(ggplot2)p <- ggplot(iris,aes(x=Sepal.Length))+geom_histogram()p
增加参数p <- ggplot(iris,aes(x=Sepal.Length))+geom_histogram(binwidth=0.1, # 设置组距fill='skyblue', # 设置填充色colour='black')+ # 设置边框色labs(title='大标题',x='x轴',y='y轴')
# 直方图的作用主要是展现分组计数和分布特性,因为考查一个样本是否符合# 某个分布在传统统计学中有着重要的意义。# 不过有另一种方法也可以展现数据的分布,即核密度估计曲线。# 简单来说就是根据数据估算一条可以代表其分布的密度曲线。# 我们可以将直方图和密度曲线重叠显示。
直方图加密度曲线p <- ggplot(iris,aes(x=Sepal.Length)) +geom_histogram(aes(y=..density..),fill='skyblue',color='black') +stat_density(geom='line',color='red',linetype=2,adjust=2)
# adjust参数和窗宽参数类似,它将会控制密度曲线的细节表现能力。我们来尝试用不同的参数绘制多条密度曲线。可见参数越小曲线越为波动灵敏,adjuct负责控制这个参数。
p <- ggplot(iris,aes(x=Sepal.Length)) +geom_histogram(aes(y=..density..), # 注意要将y设为相对频数fill='gray60',color='gray') +stat_density(geom='line',color='black',linetype=1,adjust=0.5)+stat_density(geom='line',color='black',linetype=2,adjust=1)+stat_density(geom='line',color='black',linetype=3,adjust=2)
密度曲线
密度曲线还方便对不同数据进行比较,例如我们要对iris中三种不同花的Sepal.Length分布进行比较,可以象下面一样:
p <- ggplot(iris,aes(x=Sepal.Length,fill=Species)) +geom_density(alpha=0.5,color='gray')print(p)
箱线图# 除了直方图和密度图,还可以用常见的箱线图来表现一维数据的分布。箱线图也方便各组数据之间的比较。
p <- ggplot(iris,aes(x=Species,y=Sepal.Length,fill=Species)) +geom_boxplot()print(p)
小提琴图# 与箱线图类似是小提琴图,它包含了更多关于数据分布的情况
p <- ggplot(iris,aes(x=Species,y=Sepal.Length,fill=Species)) +geom_violin()print(p)