常用的圖形,這里給出案例:
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(table(cyl),horiz = TRUE)
plot(as.factor(cyl))
plot(factor(cyl,levels = c(6,4,8)))
堆砌與分組條形圖
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))
barplot(counts,
xlab="gear",ylab="frequency",
col = c("red","yellow","green"),
legend=rownames(counts),
beside=TRUE)
添加標(biāo)簽
legend(locator(1),title("title"),
rownames(counts),
lty=c(1,2),pch=c(15,17),
col=c("red","yellow","green"))
均值條形圖
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,)
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)
棘狀圖
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)
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)))
library(plotrix)
pie3D(slices,labels=labels,explode=0.1)
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)
扇形圖
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)
核密度圖
plot(density(mpg))
par(mfrow=c(2,1))
d = density(mpg)
plot(d)
plot(d)
polygon(d,col="red",border="blue")
rug(mpg,col="brown")
par(opar)
par(lwd=2)
library(sm)
cyl.f = factor(cyl,
levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
sm.density.compare(mpg,cyl,
xlab="miles per gallon")
colfill = c(2:(1+length(levels(cyl.f))))
legend(locator(1),levels(cyl.f),fill=colfill)
legend(locator(1),levels(cyl.f),fill=rainbow(3))
箱線圖
boxplot(mpg)
boxplot(mpg~cyl,data=mtcars)
boxplot(mpg~cyl,data=mtcars,
varwidth=TRUE,
notch=TRUE)
cyl.f = factor(cyl,levels=c(4,6,8))
am.f = factor(am,levels=c(0,1),labels=c("auto","stand"))
boxplot(mpg~am.f*cyl.f,
varwidth=TRUE,
col=c("gold","darkgreen"))
小提琴圖
library(vioplot)
vioplot(mpg[cyl==4],mpg[cyl==6],mpg[cyl==8],col="gold",
names=c("4","6","8"))
title("main",xlib="x",ylab="y")
點圖
dotchart(mpg,labels=rownames(mtcars),cex=0.5)
x = mtcars[order(mtcars$mpg),]
x
mpg cyl disp hp drat wt qsec
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90
Valiant 18.1 6 225.0 105 2.76 3.460 20.22
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90
vs am gear carb manuf
Cadillac Fleetwood 0 0 3 4 Cadillac
Lincoln Continental 0 0 3 4 Lincoln
Camaro Z28 0 0 3 4 Camaro
Duster 360 0 0 3 4 Duster
Chrysler Imperial 0 0 3 4 Chrysler
Maserati Bora 0 1 5 8 Maserati
Merc 450SLC 0 0 3 3 Merc
AMC Javelin 0 0 3 2 AMC
Dodge Challenger 0 0 3 2 Dodge
Ford Pantera L 0 1 5 4 Ford
Merc 450SE 0 0 3 3 Merc
Merc 450SL 0 0 3 3 Merc
Merc 280C 1 0 4 4 Merc
Valiant 1 0 3 1 Valiant
Hornet Sportabout 0 0 3 2 Hornet
Merc 280 1 0 4 4 Merc
Pontiac Firebird 0 0 3 2 Pontiac
Ferrari Dino 0 1 5 6 Ferrari
Mazda RX4 0 1 4 4 Mazda
Mazda RX4 Wag 0 1 4 4 Mazda
Hornet 4 Drive 1 0 3 1 Hornet
Volvo 142E 1 1 4 2 Volvo
Toyota Corona 1 0 3 1 Toyota
Datsun 710 1 1 4 1 Datsun
Merc 230 1 0 4 2 Merc
Merc 240D 1 0 4 2 Merc
Porsche 914-2 0 1 5 2 Porsche
Fiat X1-9 1 1 4 1 Fiat
Honda Civic 1 1 4 2 Honda
Lotus Europa 1 1 5 2 Lotus
Fiat 128 1 1 4 1 Fiat
Toyota Corolla 1 1 4 1 Toyota
x$cyl = factor(x$cyl)
x
mpg cyl disp hp drat wt qsec
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90
Valiant 18.1 6 225.0 105 2.76 3.460 20.22
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90
vs am gear carb manuf
Cadillac Fleetwood 0 0 3 4 Cadillac
Lincoln Continental 0 0 3 4 Lincoln
Camaro Z28 0 0 3 4 Camaro
Duster 360 0 0 3 4 Duster
Chrysler Imperial 0 0 3 4 Chrysler
Maserati Bora 0 1 5 8 Maserati
Merc 450SLC 0 0 3 3 Merc
AMC Javelin 0 0 3 2 AMC
Dodge Challenger 0 0 3 2 Dodge
Ford Pantera L 0 1 5 4 Ford
Merc 450SE 0 0 3 3 Merc
Merc 450SL 0 0 3 3 Merc
Merc 280C 1 0 4 4 Merc
Valiant 1 0 3 1 Valiant
Hornet Sportabout 0 0 3 2 Hornet
Merc 280 1 0 4 4 Merc
Pontiac Firebird 0 0 3 2 Pontiac
Ferrari Dino 0 1 5 6 Ferrari
Mazda RX4 0 1 4 4 Mazda
Mazda RX4 Wag 0 1 4 4 Mazda
Hornet 4 Drive 1 0 3 1 Hornet
Volvo 142E 1 1 4 2 Volvo
Toyota Corona 1 0 3 1 Toyota
Datsun 710 1 1 4 1 Datsun
Merc 230 1 0 4 2 Merc
Merc 240D 1 0 4 2 Merc
Porsche 914-2 0 1 5 2 Porsche
Fiat X1-9 1 1 4 1 Fiat
Honda Civic 1 1 4 2 Honda
Lotus Europa 1 1 5 2 Lotus
Fiat 128 1 1 4 1 Fiat
Toyota Corolla 1 1 4 1 Toyota
x$color = ifelse(x$cyl==4,"red",
ifelse(x$cyl==6,"blue","darkgreen"))
x
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90
Valiant 18.1 6 225.0 105 2.76 3.460 20.22
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90
vs am gear carb manuf color
Cadillac Fleetwood 0 0 3 4 Cadillac darkgreen
Lincoln Continental 0 0 3 4 Lincoln darkgreen
Camaro Z28 0 0 3 4 Camaro darkgreen
Duster 360 0 0 3 4 Duster darkgreen
Chrysler Imperial 0 0 3 4 Chrysler darkgreen
Maserati Bora 0 1 5 8 Maserati darkgreen
Merc 450SLC 0 0 3 3 Merc darkgreen
AMC Javelin 0 0 3 2 AMC darkgreen
Dodge Challenger 0 0 3 2 Dodge darkgreen
Ford Pantera L 0 1 5 4 Ford darkgreen
Merc 450SE 0 0 3 3 Merc darkgreen
Merc 450SL 0 0 3 3 Merc darkgreen
Merc 280C 1 0 4 4 Merc blue
Valiant 1 0 3 1 Valiant blue
Hornet Sportabout 0 0 3 2 Hornet darkgreen
Merc 280 1 0 4 4 Merc blue
Pontiac Firebird 0 0 3 2 Pontiac darkgreen
Ferrari Dino 0 1 5 6 Ferrari blue
Mazda RX4 0 1 4 4 Mazda blue
Mazda RX4 Wag 0 1 4 4 Mazda blue
Hornet 4 Drive 1 0 3 1 Hornet blue
Volvo 142E 1 1 4 2 Volvo red
Toyota Corona 1 0 3 1 Toyota red
Datsun 710 1 1 4 1 Datsun red
Merc 230 1 0 4 2 Merc red
Merc 240D 1 0 4 2 Merc red
Porsche 914-2 0 1 5 2 Porsche red
Fiat X1-9 1 1 4 1 Fiat red
Honda Civic 1 1 4 2 Honda red
Lotus Europa 1 1 5 2 Lotus red
Fiat 128 1 1 4 1 Fiat red
Toyota Corolla 1 1 4 1 Toyota red
dotchart(x$mpg,
labels=rownames(x),cex=0.5,
groups=x$cyl,color=x$color,
pch=19)