基本图形

 

barplot条形图

barplot(height, ...)

## Default S3 method:

barplot(height, width = 1, space = NULL,

        names.arg = NULL, legend.text = NULL, beside = FALSE,

        horiz = FALSE, density = NULL, angle = 45,

        col = NULL, border = par("fg"),

        main = NULL, sub = NULL, xlab = NULL, ylab = NULL,

        xlim = NULL, ylim = NULL, xpd = TRUE, log = "",

        axes = TRUE, axisnames = TRUE,

        cex.axis = par("cex.axis"), cex.names = par("cex.axis"),

        inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,

        add = FALSE, args.legend = NULL, ...)

height是一个向量或者一个矩阵

width 是bar宽度向量,除非指定了xlim,否则指定单个值将没有任何可见的效果。

space 每个bar前面空出的空间。If height is a matrix and beside is TRUE,那么空间可以由两个数字指定,其中第一个是相同组中的条之间的空间,第二个是组之间的空间。

beside   

a logical value. If FALSE, the columns of height are portrayed as stacked bars, and if TRUE the columns are portrayed as juxtaposed bars.【见例子中的区别】

 

 

> library(vcd)
载入需要的程辑包:grid
> counts<- table(Arthritis$Improved)
> counts
 
  None   Some Marked 
    42     14     28 
> barplot(counts, main="simple bar plot")

 

> barplot(counts, main="simple bar plot", horiz = TRUE)

 

horiz = TRUE就是上面两个图的区别

 

> opar <- par(no.readonly = TRUE)
> par(mfrow=c(2,1))
> barplot(counts, main="simple bar plot")
> barplot(counts, main="simple bar plot", horiz = TRUE)
> par(opar)

 

> opar <- par(no.readonly = TRUE)
> par(mfrow=c(1,2))
> barplot(counts, main="simple bar plot")
> barplot(counts, main="simple bar plot", horiz = TRUE)
> par(opar)

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

> counts

       

         Placebo Treated

  None        29      13

  Some         7       7

  Marked       7      21

> counts1 <- table(Arthritis$Improved, Arthritis$Treatment)

> counts2 <- table(Arthritis$Treatment, Arthritis$Improved)

> opar<- par(no.readonly = TRUE)

> par(mfrow=c(2,2))

> barplot(counts1, xlab="Treatment", ylab="Improved", legend=rownames(counts1))

> barplot(counts1, xlab="Treatment", ylab="Improved", legend=rownames(counts1), beside=TRUE)

> barplot(counts2, xlab="Treatment", ylab="Improved", legend=rownames(counts2))

> barplot(counts2, xlab="Treatment", ylab="Improved", legend=rownames(counts2), beside=TRUE)

> par(opar)

 

 

 

barplot with aggregate均值条形图

此时就是要使用aggregate来计算每个不同分组的均值

 

> means <- aggregate(sta$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
> means <- aggregate(sta$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 , main = "order meanx")

 

spine棘状图

使用vcd包中的spine()绘制

> library(vcd)

> attach(Arthritis)

> counts <- table(Treatment, Improved)

> spine(counts, main = "Spinogram Example")

> detach(Arthritis)

 

 

pie/fan.splot饼图

pie(x, labs)

 

par(mfrow=c(2,2))

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

lbls <- c("US", "UK", "Asutralia", "Gremany", "France")

pie(slices, labels = lbls, main = "pie chart")

 

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

lbl2 <- paste(lbls, " :", pct, seq="")

pie(pct, labels = lbl2, col=rainbow(length(lbl2)), main = "pct pie chart")

 

install.packages("plotrix")

library(plotrix)

pie3D(slices, labels=lbl2, explode=0.1, labelcex=0.8, main="3D pie char")

 

mytable<- table(state.region)

pie(mytable, labels = names(mytable), main="table to pie chart")

 

 

 

fan.plot扇形图

 

slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Asutralia", "Gremany", "France")
fan.plot(slices, labels = lbls, main="Fan plot chart")

 

> lbls <- paste(lbls, ": ", slices, seq=" ")
> lbls
[1] "US :  10  "       "UK :  12  "       "Asutralia :  4  "
[4] "Gremany :  16  "  "France :  8  "   
> fan.plot(slices, labels = lbls, main="Fan plot chart")

 

 

 

hist直方图

hist(x)

x是一个有数据值组成的数据向量

参数freq=FALSE,表示根据概率密度二不是频数绘制图形

breaks 用于控制组的数量

par(mfrow=c(2,2))

attach(mtcars)

hist(mpg)

hist(mpg, breaks = 12, xlab = "Mile per Gallon")

 

hist(mpg, freq=FALSE, breaks = 12, col="red", xlab = "Mile per Gallon")

rug(jitter(mpg))

lines(density(mpg), col="blue", lwd=2)

h <- hist(mpg, breaks = 12, col = "red")

xfit<-seq(min(mpg), max(mpg), length=40)

yfit <- dnorm(xfit, mean =mean(mpg), sd = sd(mpg))

yfit <- yfit*diff(h$mids[1:2]*length(mpg))

lines(xfit, yfit, col="blue", lwd=2)

 

box()

 

 

 

plot(density())核密度图

核密度估计适用于估计随机便利概率密度函数的一种非参数方法

核密度图不失为一种用来观测连续型变量分布的有效方法

绘制核密度图的方法(不叠加到另一幅图上方)为:

plot(density())

 

attach(mtcars)

par(mfrow=c(3,1))

d <- density(mpg)

plot(d)

 

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

polygon(d, col="red", border="blue")

 

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

polygon(d, col="red", border="blue")

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

这三幅图中能很好的区分每个函数是干嘛的

polygon 根据x与y坐标绘制多边形,是个中间区域染色,形成密度块

rug是刻度须函数

 

使用sm包中的sm.density.compare()函数可以向图形叠加两组或者更多的核密度图

sm.density.compare(x, factor)

x是一个数值型向量,factor是一个分组变量

par(lwd=2)

library(sm)

attach(mtcars)

cyl.f <- factor(cyl, levels=c(4,6,8), labels = c("4 cyl", "6 cyl", "8 cyl"))

sm.density.compare(mpg, cyl)

colfill <- c(2:(1+length(levels(cyl.f))))

colfill

legend(locator(1), levels(cyl.f), fill=colfill)

locator(1) 是通过单击鼠标来确定legend的位置

这个legend是通过单击鼠标确定的位置

 

 

 

boxplot箱线图

箱线图通过绘制连续型变量的5个数总括,即最小值,25%, 50%, 75% , 最大值,描述了连续型变量的分布。

箱线图能够显示出可能为离群点(范围±1.5*IQR以外的值,IQR表示4分为距,即上四分位与下四分位数的差值)的观测值

boxplot(x, ...)
 
## S3 method for class 'formula'
boxplot(formula, data = NULL, ..., subset, na.action = NULL,
        drop = FALSE, sep = ".", lex.order = FALSE)
 
## Default S3 method:
boxplot(x, ..., range = 1.5, width = NULL, varwidth = FALSE,
        notch = FALSE, outline = TRUE, names, plot = TRUE,
        border = par("fg"), col = NULL, log = "",
        pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5),
        horizontal = FALSE, add = FALSE, at = NULL)
boxplot(mpg~cyl, data=mtcars)

按照cyl进行分组统计data=mtcars的数据

boxplot(formula, data=dataframe)

formula 是一个公式

dataframe代表提供数据的数据库或列表

一个公式 y~A 这将为类别变量A的每个值并列地生成变量y的箱线图

公式y~ A*B 则将为类别型变量A和B所有水平的两两组合生产数值型变量y的箱线图

varwidth= TRUE

将使用箱线图的宽度与其样本大小的平方根成正比

参数horizontal =TRUE 可以反转坐标轴的方向

notch=TRUE 则是箱线图中生成凹槽, 中位数

boxplot(mpg~cyl, data=mtcars, notch=TRUE col=”red”)

boxplot(mpg~cyl*am, data=mtcars,  col="red")

 

vioplot小提琴图

vioplot

library(vioplot)

 x1 <- mpg[cyl=4]

 

 x1 <- mpg[cyl==4]

 x1

[1] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4

 x2 <- mpg[cyl ==6]

 x3 <- mpg[cyl ==8]

 vioplot(x1, x2, x3, names = c("4 cyl", "6 cyl", "8 cyl"))

 vioplot(x1, x2, x3, names = c("4 cyl", "6 cyl", "8 cyl"), col="gold")

小提琴图时候箱线图与核密图的结合

x1, x2…..表示要绘制的一个或者多个向量

参数names 是小提琴图中标签的字符向量

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

 

 

 

dotchart点图

dotchart(x, labels)

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/guduchangjian/article/details/85272061
今日推荐