R语言绘制核密度图,箱线图,小提琴图,点图
2022-05-13 本文已影响0人
Z_bioinfo
数据
> a = runif(300)
模型
plot(density(x))
核密度图
par(mfrow = c(2,1))
> d = density(a)
> #添加标题
> plot(d, main = "kernel density")
> #将曲线修改为蓝色,实心红色填充下方区域
> polygon(d, col = "red", border = "blue")
image.png
可比较的核密度图
格式
sm.density.compare(x, factor)
数据,R包自带的数据集
> library(datasets)
> data(mtcars)
str(mtcars)
mtcars = data.frame(mtcars)
> install.packages("sm")
> library(sm)
> mtcars$cyl.f = factor(mtcars$cyl, levels = c(4,6,8), labels = c("4 cyl", "6 cyl", "8 cyl"))
> sm.density.compare(mtcars$mpg, mtcars$cyl, xlab = "miles per gallon")
> #添加标题
> title(main = "mpg distribution")
添加图例
> colfill = c(2:(1+length(levels(mtcars$cyl.f))))
> legend(locator(1), levels(mtcars$cyl.f), fill = colfill)
image.png
箱线图
简单箱线图
> boxplot(mtcars$mpg, main = "box plot", ylab = "miles per gallon")
image.png
并列箱线图进行跨组比较
格式
boxplot(formula, data = dataframe)
> boxplot(mpg ~ cyl, data = mtcars, main = "car mileage data", xlab = "number of cyl", ylab = "miles per gallon")
> boxplot(mpg ~ cyl, data = mtcars, notch = TRUE,varwidth = TRUE,col = "red", main = "car mileage data", xlab = "number of cyl", ylab = "miles per gallon" )
image.png
image.png
交叉因子箱线图
> 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(mtcars$mpg ~ mtcars$am.f*mtcars$cyl.f, data = mtcars,varwidth = TRUE,col = c("gold", "darkgreen"), main = "car mileage data", xlab = "auto type", ylab = "miles per gallon" )
image.png
小提琴图
格式
vioplot(x1,x2,..., names=,col=)
> install.packages("vioplot")
> library(vioplot)
image.png
点图
格式
dotchart(x, labels=)
> dotchart(mtcars$mpg, labels = row.names(mtcars), cex = .7, main = "gas mile for car models", xlab = "miles per millon")
> x = mtcars[order(mtcars$mpg),]
> x$cyl = factor(x$cyl)
> x$color[x$cyl==4]="red"
> x$color[x$cyl==6]="blue"
> x$color[x$cyl==8]="darkgreen"
> dotchart(x$mpg, labels = row.names(x), cex = .7, groups=x$cyl,gcolor = "black",color = x$color,pch = 19,main = "gas mile for car models", xlab = "miles per millon")
image.png
image.png