R语言:图形

本文介绍了R语言中各种常见的图形绘制,包括条形图(单向量、堆砌与分组、均值条形图)、棘状图、饼图、扇形图、直方图、核密度图、箱线图、小提琴图和点图,详细阐述了每种图形的使用场景和案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用的图形,这里给出案例:

barplot

条形图、单向量条形图

> attach(mtcars)
The following object is masked from package:ggplot2:

    mpg

> names(mtcars)
 [1] "mpg"   "cyl"   "disp"  "hp"    "drat"  "wt"   
 [7] "qsec"  "vs"    "am"    "gear"  "carb"  "manuf"
> barplot(table(cyl),
+         main="main",xlab="x",ylab="y")

barplot

barplot(table(cyl),horiz = TRUE)

水平的

plot(as.factor(cyl))

barplot2

plot(factor(cyl,levels = c(6,4,8)))

barplot3

堆砌与分组条形图

counts = table(cyl,gear)
counts

   gear
cyl  3  4  5
  4  1  8  2
  6  2  4  1
  8 12  0  2

barplot(counts,
        xlab="gear",ylab="frequency",
        col = c("red","yellow","green"),
        legend=rownames(counts))

barplot4

barplot(counts,
        xlab="gear",ylab="frequency",
        col = c("red","yellow","green"),
        legend=rownames(counts),
        beside=TRUE)

barplot5

添加标签

legend(locator(1),title("title"),
       rownames(counts),
       lty=c(1,2),pch=c(15,17),
       col=c("red","yellow","green"))

barplot6

均值条形图

means = aggregate(mpg,by=list(cyl),mean)
means 

 Group.1        x
1       4 26.66364
2       6 19.74286
3       8 15.10000

means = means[order(means$x,decreasing = TRUE),]
means

Group.1        x
1       4 26.66364
2       6 19.74286
3       8 15.10000

barplot(means$x,names.arg=means$Group.1)
title("Mean Rate")
lines(means$x,)

barplot7

opar = par(no.readonly=TRUE)
par(mar=c(5,8,4,2))
par(las=1)
counts = table(cyl)
barplot(counts,
        main="mtcars cyl",
        horiz=TRUE,
        cex.names=2,
        names.arg=names(table(cyl)))
par(opar)

barplot8

棘状图

library(vcd)
counts = table(cyl,gear)
counts

  gear
cyl  3  4  5
  4  1  8  2
  6  2  4  1
  8 12  0  2

spine(counts,main="Spinogram Example")

棘状图

饼图

par(mfrow=c(2,2))
slices = c(10,12.4,16,8)
labels = c("1","2","3","4")
pie(slices,labels)

饼图_1

pct = round(slices/sum(slices)*100)
pct

[1] 22 27 34 17
labels = paste(labels," ",pct,"%",sep="")
labels

[1] "1 22%" "2 27%" "3 34%" "4 17%"

pie(slices,labels,col=rainbow(length(pct)))

饼图_2

library(plotrix)
pie3D(slices,labels=labels,explode=0.1)

3D_饼图

counts = table(cyl)
counts

cyl
 4  6  8 
11  7 14 
lbls = paste(names(counts),"\n",counts,sep="")
lbls

[1] "4\n11" "6\n7"  "8\n14"
pie(counts,labels=lbls)
par(opar)

饼图2

扇形图

library(plotrix)
fan.plot(slices,labels=labels)
par(opar)

扇形图

直方图

par(mfrow=c(2,2))
#1
hist(mpg)
#2 add breaks and color
hist(mpg,
     breaks=12,
     col="red",
)
#3 add jitter, plot according to density rather than frequency
hist(mpg,
     breaks=12,
     col="red",
     freq=FALSE 
)
rug(jitter(mpg))
lines(density(mpg),col="blue",lwd=2)
#4 add norm curve and framework
x = mpg
h = hist(mpg,
         breaks=12,
         col="red", 
)
xfit = seq(min(x),max(x),length=40)
yfit = dnorm(xfit,mean=mean(x),sd=sd(x))
yfit1 = yfit*diff(h$mids[1:2])*length(x)
lines(xfit,yfit1,col="blue",lwd=2)
box()
par(opar)

hist

核密度图

plot(density(mpg))

密度图_1

par(mfrow=c(2,1))
d = density(mpg)
plot(d)
plot(d)
polygon(d,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值