R数据分析3
程序员文章站
2022-03-23 09:20:54
...
R语言练习题
练习题:对序列1到10,求出所有偶数的平方,并输出表达式,如2*2
对序列1到10,求出所有偶数的平方,并输出表达式,如2*2
#(1)
pow <-function(x,y){
result <-x^2
print(paste(x,"^",2,"=",result))
}
num <-1:10
for(i in num){
if(i%%2==0){
sapply(i,pow)
}
}
#(2)
num <-1:10
my.fun <-function(x){
x*x
}
for(i in num){
if(i%%2==0){
print(paste(i,"^",i,"=",my.fun(i)))
}
}
R绘制图形
主题颜色
barplot(rep(1,7),col=rainbow(7),main="barplot(rep(1,7),col=rainbow(7))")
barplot(rep(1,7),col=heat.colors(7),main="barplot(rep(1,7),col=heat.colors(7))")
barplot(rep(1,7),col=terrain.colors(7),main="barplot(rep(1,7),col=terrain.colors(7))")
barplot(rep(1,7),col=topo.colors(7),main="barplot(rep(1,7),col=topo.colors(7))")
barplot(rep(1,7),col=cm.colors(7),main="barplot(rep(1,7),col=cm.colors(7))")
文字元素
plot(0:4,type="n",axes = F ,xlab = NA ,ylab = NA)
plot(0:4,type="n",axes = T ,xlab = 'x' ,ylab = 'y')
type <- c("正常字体(默认)","粗体字体","斜体字体","粗斜体字体")
?text
for (i in 1:4){
text(2,5-i,labels = paste0 ("font=",i,":",type[i]),font = i)
}
#大小
plot(0:5,type="n",axes = F ,xlab = NA ,ylab = NA )
text(2,5,labels="cex=0.5:放大0.5倍",cex=0.5)
text(2,4,labels="cex=0.8:放大0.8倍",cex=0.8)
text(2,3,labels="cex=1(默认):正常大小",cex=1)
text(2,2,labels="cex=1.5:放大1.5倍",cex=1.5)
text(2,1,labels="cex=2:放大2倍",cex=2)
低级绘图函数
箱线图
str(iris)
attach(iris)
boxplot(Sepal.Length~Species,col=heat.colors(3),
main=list("Sepal.Length按照Species分类的箱线图",
font=4,col="green",cex=1.5),
sub=list("数据来源:iris数据集",font=3,
col="yellow",cex=0.8),
xlab="Species",ylab="Sepal.Length")
分组柱状图
head(VADeaths)
?VADeaths
barplot(VADeaths,beside = T,col=cm.colors(5))
a<-women[1:2]
a<-as.matrix(a)
barplot(a,beside = TRUE, col=topo.colors(22),main="plot women with grid (NA, 7, 1ty=2, 1wd=1.8, col='pink') ")
grid(NA,7,lty=2,lwd=1.8,col="red")
par(mfcol=c (1,1))
散点图
head(women)
nrow(women)
plot(women,main="身高 vs 体重 散点图",sub="数据来源:women数据集",
col="red",col.main="green",col.sub="blue",
col.axis="grey",col.lab="yellow")
高级绘图函数
散点图矩阵
plot(iris[,1:4],main="利用plot函数绘制散点图矩阵")
利用plot与smoothScatter函数绘制散点图
n <- 10000
x1 <- matrix(rnorm(n),ncol=2)
head(x1)
x2 <- matrix(rnorm(n,mean = 3,sd = 1.5),ncol = 2)
x <- rbind(x1,x2)
# 利用plot与smoothScatter函数绘制散点图
par(mfrow=c(1,2))
plot(x,main="利用pairs函数绘制散点图矩阵")
smoothScatter(x,main="利用smoothScatter函数绘制高密度散点图")
par(mfrow=c(1,1))
利用symbols函数绘制气泡图
x<- rnorm(5);x
y<- rnorm(5);y
r<-abs(rnorm(5));r
symbols(x,y,circles = r,inches = F,
bg=rainbow(5))
画基础图形
#画图
plot(1:5,1:5,type ="n",xlim = c(0,6),ylim=c(0,6))
symbols(x=1:5,y=1:5,circles = rep(1,5),inches = F,add=T)
#画方框
plot(1:5,1:5,type = "n",xlim = c(0,6),ylim = c(0,6))
symbols(x=1:5,y=1:5,squares = rep(1,5),inches = F,add=T)
#画长方形
symbols(x = 1:5, y = 1:5, squares = rep(2, 5), inches = FALSE, add = TRUE)
#画长方形
plot(1:5, 1:5, type = "n", xlim = c(0, 6), ylim = c(0, 6))
symbols(x = c(1, 4), y = c(3, 4), rectangles = matrix(c(2, 3, 3, 2), byrow = T, nrow = 2),
inches = FALSE, add = TRUE)
箱线图
install.packages("ggplot2")
library(ggplot2)
ggplot(iris,aes(x=Species,y=Sepal.Length,fill=Species))+
geom_boxplot()+
labs(title="依据种类分组的花萼长度箱线图")
#利用ggplot函数绘制小提琴图
ggplot(iris,aes(x=Species,y=Sepal.Length,fill=Species))+
geom_violin()+
geom_jitter()+
labs(title="依据种类分组的花萼长度箱线图")
运行结果图:
哆啦A梦欢迎您的到来!