#第六章基本图形
#条形图
barplot(height)
library(vcd)
Arthritis
counts <- table(Arthritis$Improved)
barplot(counts,
main = "Count",
xlab = "improve",ylab = "frequency",
horiz = TRUE)
count1 <- table(Arthritis$Improved,Arthritis$Treatment)
#柱状图 堆砌条形图
barplot(count1,
main = "stacked",
xlab = "improve",ylab = "frequency",
col = c("red","orange","green"),
legend = rownames(count1))
#柱状图 分组条形图
barplot(count1,
main = "Grouped",
xlab = "improve",ylab = "frequency",
col = c("red","orange","green"),
legend = rownames(count1),
beside = TRUE)
#均值条形图
states <- data.frame(state.region,state.x77)
means <- aggregate(states$Illiteracy,by=list(state.region),FUN = mean,rm.na=TRUE)
means <- means[order(means$x),]
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 improve","Some improve","Marked improve"))
#棘状图 对堆砌图进行重缩放,使每一个条形的高度均为1
library(vcd)
attach(Arthritis)
counts <- table(Treatment,Improved)
spine(counts,main = "Example")
detach(Arthritis)#饼图
par(mfrow=c(2,2))#将下面绘制的四幅图放在一个面板
slices <- c(10,12,4,16,8)
lbls <- c("US","UK","Australia","Germany","France")
pie(slices,labels = lbls,
main = "1 simple")pct <- round(slices/sum(slices)*100)
lbls2 <- paste(lbls," ",pct,"%",sep = "")
pie(slices,labels = lbls2,
col = rainbow(length(lbls2)),
main = "2 Percenyage")library(plotrix)
pie3D(slices,labels = lbls,explode = 0.1,
main = "3 3D")mytable <- table(state.region)
lbls3 <- paste(names(mytable),"\n",mytable,sep="")
pie(mytable,labels = lbls3,
main = "4 table to pic")
#扇形图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轴显示相应频数
#freq=FALSE表示根据概率密度而不是频数绘制图形
#breaks用于控制组的数量
par(mfrow=c(2,2))#将下面绘制的四幅图放在一个面板
hist(mtcars$mpg,
main = "1")hist(mtcars$mpg,
main = "2",
breaks = 12,
col = "forestgreen",
xlab = "Miles Per Gallon")hist(mtcars$mpg,
main = "3",
breaks = 12,
col = "forestgreen",
freq = FALSE,
xlab = "Miles Per Gallon")
rug(jitter(mtcars$mpg))#添加轴须图
lines(density(mtcars$mpg),col="blue",lwd=2)x <- mtcars$mpg
h <- hist(x,
main = "4",
breaks = 12,
col = "orange",
xlab = "Miles Per Gallon")
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()#核密度图
#plot函数会创建一幅新的图,所以可以用lines函数向已经存在的图形上添加密度曲线
par(mfrow=c(2,1))
d <- density(mtcars$mpg)
plot(d)plot(d,main = "核密度图")
polygon(d,col="red",border = "blue")#
rug(mtcars$mpg,col = "brown")#箱线图
boxplot(mtcars$mpg,
main="Box plot",
ylab="Miles per Gallon")
boxplot.stats(mtcars$mpg)#可以知道不同分位数对应数据
boxplot(mpg~cyl,data = mtcars,
main="Car Mileage Data",
xlab = "Number of Cylinders",
ylab = "Miles Per Gallon")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("au","st"))boxplot(mpg~am.f*cyl.f,
data = mtcars,
varwidth=TRUE,
col=c("gold","forestgreen"),
main="MPG",
xlab = "Auto type",
ylab = "Miles Per Gallon")
#小提琴图
library(vioplot)
R语言实战-第六章基本图形
于 2023-01-08 12:05:14 首次发布