R语言实战笔记

这篇博客详细介绍了R语言中的各种图形绘制方法,包括条形图、棘状图、直方图、核密度图、箱线图、点图、气泡图、相关图以及OLS回归等。同时,讲解了如何进行多元线性回归分析,包括变量相关性检验、简单线性回归、回归诊断、向后回归和全子集回归。此外,还探讨了广义线性模型中的Logistic回归。内容深入浅出,结合实例展示,适合R语言初学者和进阶者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

par(no.readonly=TRUE) 修改当前图形参数,会话结束前一直有效。

符号与线条:pch 绘制点时使用的符号;cex 符号大小;lty 线条类型;ldy 线条宽度

legend 图例标签

基本图形

条形图

barplot(height, width = 1, space = NULL,

       names.arg = NULL, legend.text = NULL, beside = FALSE,

       horiz = FALSE, density = NULL, angle = 45,

       col = NULL, border = par("fg"),

       main = NULL, sub = NULL, xlab = NULL, ylab = NULL,

       xlim = NULL, ylim = NULL, xpd = TRUE, log = "",

       axes = TRUE, axisnames = TRUE,

       cex.axis = par("cex.axis"), cex.names = par("cex.axis"),

       inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,

       add = FALSE, args.legend = NULL, ...)

beside为F,则为堆砌条形图,为T,则为分组条形图

棘状图

library(vcd)

attach(Arthritis)

counts<-table(Treatment,Improved)

spine(counts,main="Spinogram")

R语言实战笔记

直方图

hist(mtcars$mpg,freq=F,breaks=12,col="red",xlab="Miles perGallon",main="Histogram,rug plot,density curve")

rug(jitter(mtcars$mpg))

lines(density(mtcars$mpg),col="blue",lwd=2)#density为核密度图

freq

logical; if TRUE, the histogramgraphic is a representation of frequencies,thecounts component of the result;if FALSE, probability densities, componentdensity,are plotted (so that the histogram has a total area of one).Defaults to TRUE if and onlyif breaks are equidistant(and probability is notspecified).

 R语言实战笔记

核密度图

par(lwd=2)  #双倍线条宽度

library(sm)

attach(mtcars)

cyl.f<-factor(cyl,levels=c(4,6,8),labels=c("4cylinders","6cylinders","8cylinders"))#创建分组因子

sm.density.compare(mpg,cyl,xlab="miles per gallon")#绘制密度图

title(main="MPG Distribution by Car Cylinders")

colfill<-c(2:(1+length(levels(cyl.f)))) #通过鼠标点击添加图例

legend(locator(1),levels(cyl.f),fill=colfill)

detach(mtcars)


箱线图

boxplot(mpg~cyl,data=mtcars,main="Car Mileage Data",xlab="Numberof Cylinders",ylab="Miles perGallon")


mtcars$cyl.f<-factor(mtcars$cyl,levels=c(4,6,8),labels=c("4","6","8"))

mtcars$am.f<-factor(mtcars$am,levels=c(0,1),labels=c("auto","standard"))

boxplot(mpg~am.f*cyl.f,data=mtcars,varwidth=T,col=c("gold","green"),main="MPGDistribution by Auto Type",xlab="Auto Type")



点图

dotchart(x,labels=)

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

x$cyl<-factor(x$cyl)

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

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

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

dotchart(x$mpg,labels=row.names(x),cex=.7,groups=x$cyl,gcolor="black",color=x$color,pch=19,main="GasMileage for Car Models",xlab="Miles per Gallon")

scatterplot(weight~height,data=women,spread=F,lty.smooth=2,pch=19,main="WomenAge 30-39",xlab="Height",ylab="Weight")


scatterplotMatrix(x, var.labels=colnames(x),

   diagonal=c("density", "boxplot", "histogram", "oned", "qqplot","none"),

    adjust=1,nclass,

   plot.points=TRUE, smoother=loessLine, smoother.args=list(), smooth,span,

    spread =!by.groups, reg.line=lm,

   transform=FALSE, family=c("bcPower", "yjPower"),

   ellipse=FALSE, levels=c(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值