R 语言可视化学习之路——基础图形

条形图

条形图通过垂直或者水平的条形展示类别变量的分布(频数)。

简单用法是barplot(height) height是一个向量或矩阵

简单条形图

数据

library("vcd")
载入需要的程辑包:grid
Warning message:
程辑包‘vcd’是用R版本4.0.5 来建造的 
counts<-table(Arthritis$Improved)
counts
  None   Some Marked 
    42     14     28 

作图代码

barplot(counts,main=“Simple Bar Plot”,xlab=‘Improvement’,ylab=“Frequency”)
barplot(counts,main=“Simple Bar Plot”,xlab=‘Improvement’,ylab=“Frequency”,horiz=TRUE)

在这里插入图片描述
在这里插入图片描述

堆砌条形图

数据

counts<-table(Arthritis I m p r o v e d , A r t h r i t i s Improved,Arthritis Improved,ArthritisTreatment)
counts

​ Placebo Treated

None 29 13
Some 7 7
Marked 7 21

作图代码

 barplot(counts,main="Stacked Bar Plot",xlab="Treatment",ylab="Frequency",col=c("red","yellow","green"),legend=rownames(counts))


在这里插入图片描述

分组条形图

 barplot(counts,main="Group Bar Plot",xlab="Treatment",ylab="Frequency",col=c("red","yellow","green"),legend=rownames(counts),beside=TRUE)

在这里插入图片描述

均值条形图

states<-data.frame(state.region,state.x77)
> means<-aggregate(states$Illiteracy,by=list(state.region),FUN=mean)
> means
        Group.1        x
1     Northeast 1.000000
2         South 1.737500
3 North Central 0.700000
4          West 1.023077
> means<-means[order(means$x),]
> means
        Group.1        x
3 North Central 0.700000
1     Northeast 1.000000
4          West 1.023077
2         South 1.737500
> barplot(means$x,names.arg=means$Group.1)
> title("Mean Illiteracy Rate")

在这里插入图片描述

条形图的微调

> par(mar=c(5,8,4,2))
> par(las=2)
> counts<-table(Arthritis$Improved)
> barplot(counts,main="Treatment Outcome",horiz=TRUE,cex.names=0.8,names.arg=c("No Improvement","Some Improvement","Marked Improvement"))

在这里插入图片描述

棘状图

棘状图对堆砌条形图进行重缩放,特点是每个条形高度均为1

需要用到vcd包中的spine()函数绘制

> library("vcd")
> attach(Arthritis)
> counts<-table(Treatment,Improved)
> spine(counts,main="Spinogram Example")
> detach(Arthritis)

在这里插入图片描述

饼图

相对于饼图,更推荐使用条形图或点图,因为相对于面积,人们对长度的判断更精确

pie(x,labels)

x是非负数值向量,代表每个扇形的面积

labels是字符型向量,代表每个扇形标签

par(mfrow=c(2,2))
slices<-c(10,12,4,16,8)
lbls<-c("US","UK","Australia","Germany","France")
pie(slices,labels=lbls,main="Simple Pie Chart")

pct<-round(slices/sum(slices)*100)
lbls2<-paste(lbls,"",pct,"%",sep=" ")
pie(slices,labels=lbls2,col=rainbow(length(lbls2)),main="Pie Chart With Percentages")

library(plotrix)
pie3D(slices,labels=lbls,explod=0.1,main="3D Pie Chart")

mytable<-table(state.region)
lbls3<-paste(names(mytable),"\n",mytable,sep=" ")
pie(mytable,labels=lbls3,main="Pie Chart from a Table\n (with simple sizes)",)

在这里插入图片描述

第二幅图:rainbow()函数定义了个扇形的颜色;将样本数转换为比例值,并将信息添加到个扇形的标签上

第三幅图:plotrix包,pie3D()函数创建三维饼图。【三维效果无法增进对数据的理解,并且是分散注意立的花哨行为】

第四幅图:从表格中创建饼图。

扇形图

扇形图是饼图变种,提供了一种同时展现相对数量和相互差异的方法。通过plotrix中的fan.plot()实现

library(plotrix)
slices<-c(10,12,4,16,8)
lbls<-c("US","UK","Australia","Germany","France")
fan.plot(slices,labels=lbls,main="Fan Plot")

在这里插入图片描述

由图可见,德国的扇形面积是最大的,相对最大。扇形图中的半径并不重要,宽度最重要

直方图

直方图通过在x轴上将值域分割为一定数量的组,在y轴显示相应值的频数,展示了连续变量的分布

hist(x)

par(mfrow=c(2,2))
hist(mtcars$mpg)

hist(mtcars$mpg,breaks=12,col='red',xlab="Miles Per Gallon",main="Colored histogram eith 12 bins")

hist(mtcars$mpg,freq=FALSE,breaks=12,col="red",xlab="Miles Per Gallon",main="Histogram,rug plot, density curve")
rug(jitter(mtcars$mpg))
lines(density(mtcars$mpg),col="blue",lwd=2)

x<-mtcars$mpg
h<-hist(x,breaks=12,col="red",xlab="Miles Per Gallon",main="Histogram with normal curve and box")
xfit<-seq(min(x),max(x),length=40)
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
yfit<-yfit*diff(h$mids[1:2])*length(x)
lines(xfit,yfit,col="blue",lwd=2)
box()

在这里插入图片描述

图1:一切默认

图2:指定组数为12,红色填充,加上标题,标签

图3:2的基础上,加上密度曲线轴须图

​ 这个密度曲线是核密度估计:是用于估计随机变量概率密度函数的一种非参数方法

​ 轴须图是实际数据值的一种呈现方式,可以查看数据的密集程度。比如图中有很多点聚集在15附近。

图4:2的基础上,加上正态曲线盒型

核密度图

plot(density(x))绘制一个核密度图

lines(density(x)) 添加一条核密度曲线

polygon()绘制多边形

par(mfrow=c(2,1))
d<-density(mtcars$mpg)
plot(d)

d<-density(mtcars$mpg)
plot(d,main="Kernel Density of Miles Per Gallon")
polygon(d,col="red",border="blue")
rug(mtcars$mpg,col="brown")

在这里插入图片描述

可比较的核密度图

就是体现出组间差异

用sm包中的sm.density.compare(x,factor)函数实现。

x:数值型向量,factor分组变量

library(sm)
attach(mtcars)
#创建分组因子
cyl.f<-factor(cyl,levels=c(4,6,8),labels=c("4 cylinder","6 cylinder","8 cylinder"))

#绘制密度图
sm.density.compare(mpg,cyl,xlab="Miles Per Gallon")
title(main="MPG  Distribution by Car Cylinders")

#通过鼠标单击添加图例
colfill<-c(2:(1+length(levels(cyl.f))))
legend(locator(1),levels(cyl.f),fill=colfill)

detach(mtcars)

在这里插入图片描述

箱线图

par(mfrow=c(1,2))
boxplot(mtcars$mpg,main="Box Plot",ylab="Miles Per Gallon")

boxplot(mpg~cyl,data=mtcars,main="Car Mileage Data",
xlab="Number of Cylinders",ylab="Miles Per Gallon")

在这里插入图片描述

含凹槽的箱线图:

boxplot(mpg~cyl,data=mtcars,notch=TRUE,
varwidth=TRUE,
col="red",
main="Car Mileage Data",
xlab="Number of Cylinders",
ylab="Miles Per Gallon")

notch=TRUE 可以得到含凹槽的箱线图,若两个凹槽互不重叠,表示中位数有差异
varwidth=TRUE表示箱线图的宽度与各自样本数量成比例

在这里插入图片描述

多个分组因子箱线图

mtcars$cyl.f<-factor(mtcars$cyl,levels=c(4,6,8),labels=c("4","6","8"))
mtcars$am.f<-factor(mtcars$am,levels=c(0,1),labels=c("auto","standard"))

boxplot(mpg~am.f*cyl.f,data=mtcars,varwidth=TRUE,col=c("gold","darkgreen"),
main="MPG Distribution by Auto Type",
xlab="Auto Type",ylab="Miles Per Gallon")

在这里插入图片描述

小提琴图

小提琴图是箱线图和核密度图的结合

使用前需要安装vioplot包

library(vioplot)
x1<-mtcars$mpg[mtcars$cyl==4]
x2<-mtcars$mpg[mtcars$cyl==6]
x3<-mtcars$mpg[mtcars$cyl==8]
vioplot(x1,x2,x3,names=c("4 cyl","6 cyl","8 cyl"),col="gold")
title("Violin Plots of Miles Per Gallon",
ylab="Miles Per Gallon",xlab="Number of Cylinders")

在这里插入图片描述

白点是中位数,黑色盒型范围是下四分位到上四分位,细黑线表示须。外部形状是核密度估计。

点图

pg[mtcars$cyl==4]
x2<-mtcars$mpg[mtcars$cyl==6]
x3<-mtcars$mpg[mtcars$cyl==8]
vioplot(x1,x2,x3,names=c("4 cyl","6 cyl","8 cyl"),col="gold")
title("Violin Plots of Miles Per Gallon",
ylab="Miles Per Gallon",xlab="Number of Cylinders")

在这里插入图片描述

白点是中位数,黑色盒型范围是下四分位到上四分位,细黑线表示须。外部形状是核密度估计。

分组、排序、着色后的点图

x<-mtcars[order(mtcars$mpg),]
x$cyl<-factor(x$cyl)
x$color[x$cyl==4]<-"red"
x$color[x$cyl==6]<-"blue"
x$color[x$cyl==8]<-"darkgreen"

dotchart(x$mpg,
labels=row.names(x),
cex=.7,
groups=x$cyl,
gcolor="black",
color=x$color,
pch=19,
main="Gas Mileage for Car Models\ngrouped by cylinder",
xlab="Miles Per Gallon")

在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值