实用统计图形

今天画的图都是简单图形,大部分EXCEL就能实现,但是当做复习记录,还是再来重述一遍。

1:饼状图(用于描述量,频率或者百分比之间的相对关系)

> countries <- c("Brazil", "Russia", "India", "China", "South Africa")
> GDP <- c(23920, 20790, 18618, 94906, 3066)
> pie(GDP, labels = countries, mian = "GDP of BRICS countries (2013)")

> pie(GDP, labels = countries, main = "GDP of BRICS countries (2013)",clockwise = T) # 显示方向次序

> pie(GDP, labels = countries, main = "GDP of BRICS countries (2013)",clockwise = T, col = gray(seq(0.4,1.0, length = 5))) # 按灰度来显示颜色


> per <- round(GDP/sum(GDP)*100, 2)
> index <- paste(countries, "", per, "%", sep = "")
> pie(GDP, labels = index, col = rainbow(length(index)), main = "Pie Chart with Percentages")

round()#四舍五入,保留两位小数点,使用rainbow()函数来定义各个扇形的颜色。

> library(plotrix)
> pie3D(GDP, labels = countries, explode = 0.1, main = "3D Pie Chart")

> fan.plot(GDP, labels = countries, main = "Fan Plot")

2:直方图

> attach(mtcars)
> mpg
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5
[22] 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
> hist(mpg)
> hist(mpg,breaks = 12, col = "lightblue", border = "pink", xlab = "Miles/Gallon", main = "Colored Histogram Example.1")
> hist(mpg,breaks = 12, col = "blue1", xlim = c(10, 35), xlab = "Miles/Gallon", main = "Colored Histogram Example.2")
参数breaks的赋值为12后,矩形条同时被等分为12个档次。

正常情况下绘制归一化直方图应该是通过将参数freq的值置为FALSE来实现。

3:核密图(用于估计随时变量概率密度的一种非参数方法,也可以用于组间差异)

> d <- density(mpg)
> plot(d)

还可以用于组间差异(箱线图也是常用的图形软件)

> plot(density(mtcars[mtcars$cyl == 4,]$mpg), col = "red", lty = 1, xlim = c(5,40), ylim = c(0,0.25),xlab = "", main = "")
> par(new = T)
> plot(density(mtcars[mtcars$cyl == 6,]$mpg), col = "blue", lty = 1, xlim = c(5,40), ylim = c(0,0.25),xlab = "", main = "")
> par(new = T)
> plot(density(mtcars[mtcars$cyl == 8,]$mpg), col = "green", lty = 1, xlim = c(5,40), ylim = c(0,0.25),xlab = "", main = "MPG Distribution by Cylinders")
> par(new = T)
> legend("topright", legend = text.legend, lty = c(1,2,3), col = c("red","blue", "green"))









猜你喜欢

转载自blog.csdn.net/xiuxiu179/article/details/80757029
今日推荐