标签:拼接字符串 旋转 一个 nta library marked 参数 sep 有序
1. 条形图 barplot()
#载入vcd包 library(vcd) #table函数提取各个维度计数 counts <- table(Arthritis$Improved) counts #绘制简单Improved条形图 #1行2列 par(mfrow=c(1,2)) barplot(counts, main = "simple Bar plot", xlab = "Improved",ylab = "Frequency") #绘制水平条形图 horiz = TRUE barplot(counts,main = "simple Bar plot", xlab = "Frequency",ylab = "Improved",horiz=TRUE) #如果绘制的是一个有序因子,可以使用plot()函数快速创建一幅垂直条形图 #1行1列 par(mfrow=c(1,1)) plot(Arthritis$Improved,xlab = "Frequency",ylab = "Improved",horiz=TRUE) #如果要绘制的变量是一个矩阵而不是一个向量,将会绘制堆砌条形图或者分组条形图 #生成Improved和Treatment列联表 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) #条形图微调 #增加y边界大小 par(mar = c(5,8,4,2)) #las=2旋转条形标签 par(las = 2) counts <- table(Arthritis$Improved) #cex.names= 0.8缩小字体的大小 barplot(counts,main="Treatment Outcomes",horiz = TRUE,cex.names=0.8,names.arg = c("No Improvement","Some Improvement","Marked Imporvement"))
2. 饼图:饼图在商业世界中无所不在,然而多数统计学家,包括R相应文档的编写者,都对它持否定态度。相对于饼图,他们更推荐使用条形图或点图,因为相对于
面积,人们对长度的判断更为精确 pie() pie3D()
par(mfrow=c(2,2)) slices <- c(10,12,12.4,16,8) lbls <- c("US","UK","Austrialia","Germany","France") pie(slices,labels=lbls,main="simple Chart") pct <- round(slices/sum(slices)*100) #拼接字符串和比例数值 lbls2 <- paste(lbls," ", pct,"%",sep = "") lbls2 pie(slices,labels=lbls2,col=rainbow(length(lbls2)),main="Pie chart with percentage") #载入ploirix包 library(plotrix) #画简单3D图 pie3D(slices,labels=lbls,explode = 0.4,main = "3D pei chart") #从表格创建饼图 #table取得region的计数表 mytable <- table(state.region) #names 函数取得列名,然后将列名和相应的计数拼接在一起 lbls3 <- paste(names(mytable),‘\n‘,mytable,sep="") pie(mytable,labels=lbls3,main="pie chart from a table\n{with sample sizes}")
3. 直方图 :可以展示连续变量的分布. hist(x,breaks=, freq = )
x是一个由数据值组成的数值向量,参数freq = FALSE表示根据概率密度而不是频数绘制图形,参数breaks用于控制数组的数量.
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()
标签:拼接字符串 旋转 一个 nta library marked 参数 sep 有序
原文地址:http://www.cnblogs.com/vincentcheng/p/7896895.html