天天看点

R语言实战-读书笔记 (第6章 基础图形)

R语言实战-读书笔记 (第6章 基础图形)

***********************************

与导图结合的脚本文件:

创建脚本:文件——新建脚本程序,将以下代码复制粘贴至脚本内,选中右键运行当前或所选代码。

#第6章 基础图形#

install.packages("vcd")#载入包,使用包中的Arthritis数据#

library(vcd)

counts <- table(Arthritis$Improved)#提取各单元的计数#

counts

barplot(counts,main="Simple Bar Plot",

xlab="Improvement", ylab="Frequency")#简单条形图#

barplot(counts,main="Horizontal Bar Plot",

xlab="Frequency", ylab="Improvement",

horiz=TRUE)#水平条形图#

plot(Arthritis$Improved, main="Simple Bar Plot",

xlab="Improved", ylab="Frequency")

plot(Arthritis$Improved, horiz=TRUE, main="Horizontal Bar Plot",

xlab="Frequency", ylab="Improved")

#堆砌和分组条形图#

counts <- table(Arthritis$Improved, Arthritis$Treatment)

counts

barplot(counts,main="Stacked Bar Plot",

xlab="Treatment", ylab="Frequency",

col=c("red", "yellow","green"),

legend=rownames(counts))#堆砌#

barplot(counts,

main="Grouped Bar Plot",

xlab="Treatment", ylab="Frequency",

col=c("red", "yellow", "green"),

legend=rownames(counts), beside=TRUE)#条形#

#均值条形图#

states <- data.frame(state.region, state.x77)

head(states)

means <- aggregate(states$Illiteracy, by=list(state.region), FUN=mean)

means

means <- means[order(means$x),]#升序排序#

barplot(means$x, names.arg=means$Group.1)#means是一个数据框,不是向量和矩阵#

#means$x 是包含各条形高度的向量,而添加选项 names.arg=means$Group.1是为了展示标签。#

title("Mean Illiteracy Rate")

#条形图微调#

par(mar=c(5,8,4,2))#增加y边界大小“8”#

par(las=2)#旋转条形的标签,看X轴#

counts <- table(Arthritis$Improved)

barplot(counts,

main="Treatment Outcome",

horiz=TRUE,

cex.names=0.8,#缩小字体大小,标签Y轴#

names.arg=c("No Improvement", "Some Improvement","Marked Improvement"))

#棘状图 #

library(vcd)

attach(Arthritis)

counts <- table(Treatment, Improved)

counts

spine(counts, main="Spinogram Example",margins = c(5.1, 4.1, 4.1, 3.1))

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="Simple Pie Chart")

pct <- round(slices/sum(slices)*100)

pct

lbls2 <- paste(lbls, " ", pct, "%", sep="")

#paste(c('A','B','C','D','E'),c(1,2),sep = '-') sep为连接形式#

lbls2

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

main="Pie Chart with Percentages")

mytable <- table(state.region)

lbls3 <- paste(names(mytable), "\n", mytable, sep="")

pie(mytable, labels = lbls3,

main="Pie Chart from a Table\n (with sample sizes)")

margins = c(5.1, 4.1, 6.1, 3.1)

#扇形图#

install.packages("plottrix")

library(plotrix)

slices <- c(10, 12,4, 16, 8)

lbls <- c("US", "UK", "Australia", "Germany", "France")

fan.plot(slices, labels = lbls, main="Fan Plot")

#直方图#

par(mfrow=c(2,2))

hist(mtcars$mpg)#简单直方图#

hist(mtcars$mpg,breaks=12,col="red",

xlab="Miles Per Gallon",main="Colored histogram with 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()

#核密度图#

par(mfrow=c(2,1))

d <- density(mtcars$mpg)

plot(d)

plot(d, main="Kernel Density of Miles Per Gallon")

polygon(d, col="red", )

rug(mtcars$mpg, col="brown")

install.packages("sm")

library(sm)

par(mfrow=c(1,1))

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)

#箱线图#

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")

boxplot(mpg ~ cyl, data=mtcars,

notch=TRUE,

varwidth=TRUE,

col="red",

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("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")

#小提琴图#

install.packages("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")

#点图#

head(mtcars)

dotchart(mtcars$mpg, labels=row.names(mtcars), cex=.7,

main="Gas Mileage for Car Models",

xlab="Miles Per Gallon")

x <- mtcars[order(mtcars$mpg),]#根据每加仑汽油行驶英里数(从最低到最高)对数据框 mtcars 进行排序,结果保存为数据框 x#

head(x)

x$cyl <- factor(x$cyl)#将数值向量 cyl 转换为一个因子#

x$color[x$cyl==4] <- "red"

x$color[x$cyl==6] <- "blue"

x$color[x$cyl==8] <- "darkgreen" #添加一个字符型向量 ( color )到数据框 x 中,根据 cyl 的值,它所含的值为 "red" 、 "blue" 或 "darkgreen"#

head(x)

dotchart(x$mpg,

labels = row.names(x),

cex=.7,

groups = x$cyl,

gcolor = "black",#数字4、6和8以黑色显示#

color = x$color,#点和标签的颜色来自向量 color#

pch=19,

main = "Gas Mileage for Car Models\ngrouped by cylinder",

xlab = "Miles Per Gallon")