R语言绘制饼图,扇图,直方图

2022-05-13  本文已影响0人  Z_bioinfo

模型

pie(x, labels)

数据

> slices = c(10, 12, 4, 16, 8)
> lbs = c("US", "UK", "China", "Japan", "France")

饼图

将四幅图形组合为一幅
> par(mfrow=c(2,2))
**简单饼图**
> pie(slices, labels = lbs, main = "simple pie chart")
**添加比例数值**
> pct = round(slices/sum(slices)*100)
> lbs2 = paste(lbs, " ", pct, "%", sep = "")
> pie(slices, labels = lbs2, col = rainbow(length(lbs2)), main = "pie chart with percentages")
**三维饼图**
> install.packages("plotrix")
> library(plotrix)
> pie3D(slices, labels = lbs, explode = 0.1, main = " 3D pie chart")
**从表格创建饼图
> mytable = table(state.region)
> lbs3 = paste(names(mytable), "\n", mytable, sep = "")
> pie(mytable, labels = lbs3, main = "pie chart from a table\n (with sample sizes)")

image.png

扇形图

library(plotrix)
fan.plot(slices, labels = lbs, main = "fan plot")
image.png

直方图

数据

> a
 [1] 0.921728136 0.819813978 0.155258097 0.860796186 0.010337085 0.313979762 0.181945868
 [8] 0.191119399 0.340760427 0.990035893 0.680026217 0.021525492 0.676116193 0.550273950
[15] 0.228975511 0.215366522 0.673866837 0.539552713 0.003466371 0.249290793 0.740305768
[22] 0.350262844 0.972470085 0.233697871 0.147437298 0.161116413 0.025756740 0.857023069
[29] 0.058048269 0.719605658 0.546377301 0.762977070 0.202462197 0.778545029 0.982251617
[36] 0.985399520 0.735237126 0.245422044 0.431823339 0.331799285 0.547135432 0.227419640
[43] 0.996566102 0.624199087 0.876766883 0.904353059 0.999835051 0.109322762 0.509495925
[50] 0.720892724

作图

par(mfrow=c(2,2))
**简单直方图
> hist(a)
**指定组数和颜色
> hist(a, breaks = 12,col = "red")
**添加轴须图
> hist(a, breaks = 12,col = "red", freq = FALSE)
> rug(jitter(a))
> lines(density(a), col="blue", lwd = 2)
**添加正态密度曲线和外框
> h = hist(a, breaks = 12,col = "red")
> yfit = dnorm(xfit, mean = mean(a), sd = sd(a))
> yfit = yfit*diff(h$mids[1:2])*length(a)
> lines(xfit,yfit,col="blue",lwd = 2)
image.png
上一篇下一篇

猜你喜欢

热点阅读