R语言6章

 library(vcd) 

counts<-table(Arthritis$Improved)

简单条形图

barplot(counts,main="Simple Bar Plot",xlab="Improvement",ylab="Frequency")

堆积条形图分组条形图

library(vcd)

counts<-table(Arthritis$Improved,Arthritis$Treatment)

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)

均值条形图

states<-data.frame(state.region,state.x77)

means<-aggregate(states$Illiteracy,by=list(state.region),FUN=mean)

means<-means[order(means$x),]

barplot(means$x,names.arg=means$Group.1)

title("Mean illiteracy Rate")

条形图的微调

par(mar=c(5,8,4,2))

par(las=2)

counts<-table(Arthritis$Improved)

barplot(counts,main="Treatment Ountcome",horiz=TRUE,cex.names=0.8,names.arg=c("No Improvement","Some Improvement","Marked Improvement"))

棘状图

library(vcd)

attach(Arthritis)

counts<-table(Treatment,Improved)

spine(counts,main="Spinogram Example")

detach(Arthritis)

饼图

par(mfrow=c(2,2))

slices<-c(10,12.4,16,8)

lbls<-c("US","UK","Australia","Germany","France")

pie(slices,labels=lbls,main="Simple Pie Chart")

pct<-round(slices/sum(slices)*100)

lbls2<-paste(lbls,"",pct,"%",sep="")

pie(slices,label=lbls2,col=rainbow(length(lbls2)),

main="Pie Chart with Percentages")

library(plottrix)

pie3D(slices,labels=lbls,explode=0.1,main="3D Pie Chart")

mytable<-table(state.region)

lbls3<-paste(names(mytable),"\n",mytable,sep="")

pie(mytable,labels=lbls3,main="Pie Chart from a Table\n (with sample sizes)")

 

 

 

install.packages("plotrix")

 

library(plotrix)

slices<-c(10,12.4,16,8)

lbls<-c("US","UK","Australia","Germany","France")

fan.plot(slices,labels=lbls,main="Fan Plot")

 

 

 

直方图

par(mfrow=c(2,2))

hist(mtcars$mpg)

#指定 组数和颜色

hist(mtcars$mpg,breaks=12,col="red",xlabs="Miles Per Gallon",main="Colored histogram with 12 bins")

 

par(mfrow=c(2,2))

hist(mtcars$mpg)

hist(mtcars$mpg,breaks=12,col="red",xlabs="Miles Per Gallon",main="Colored histogram with 12 bins")

 

 

 

 #添加轴须图

hist(mtcars$mpg,freq=FALSE,breakks=12,col="green",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="pink",xlab="Miles Per Gallon",main="Histogram with normal curve and box")

xfit<-seq(min(x),max(x),length-40)

yfit<-yfit*diff(h$mids[1:2])*length(x)

lines(xfit,yfit,col="blue",lwd=2)

box()

 

 

 

 

 

箱线图

boxplot(mtcars$mpg,main="Box plot",ylab="Miles per Gallon")

 

 

 #使用并列箱线图进行跨组比较

boxplot(mpg~cyl,data=mtcars,main="Car Mileage Data",xlab="Number of Cylinders",ylab="Miles Per Gallon")

 

 

 点图

dotchart(mtcars$mpg,labels=row.names(mtcars),cex=0.7,main="Gas Mileage for Car Models",xlabs="Miles Per Gallon")

 

 

 #分组、排序、着色后的点图

x<-mtcars[order(mtcars$mpg),]

x$cyl<-factor(x$cyl)

x$color[x$cyl==4]<-"red"

x$color[x$cyl==6]<-"green"

x$color[x$cyl==8]<-"pink"

dotchart(x$mpg,

lables=row.names(x),

cex=0.7,

groups=x$cyl,

gcolor="black",

color=x$color,

pch=19,

main="Gas Mileage for Car Models\ngrouped by cylinder",xlab="Miles Per Gallon")

 

posted @   ^_^Anne  阅读(110)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示