r语言中,使用plot画图,需要画的图有点有线,那么type参数的设置为

作者&投稿:包码 (若有异议请与网页底部的电邮联系)
r语言 plot设置刻度~

解决这个问题的方法如下:
1、首先在R中,plot函数作图时会自动给出xy轴的刻度标度。

2、然后有时需要自己定义xy轴的刻度,这时可以用axis中的at和labels参数来更改,先令plot不要画出xy轴的标度。

3、最后,用axis函数设置xy轴的刻度,这样问题就解决了。

原贴转自生物统计家园,原贴只贴出了代码,没有画图,这里运行了一下代码,复习了一下R的绘图,共有20多个。
案例01-基本绘图
cars <- c(1, 3, 6, 4, 9)
plot(cars)

案例 1-基本绘图
案例02-增添标题
cars <- c(1, 3, 6, 4, 9)
plot(cars, type="o", col="blue") # 绘制蓝色折线图,
title(main="Autos", col.main="red", font.main=4) # 增添标题,红色,粗斜体

案例 2-增添标题
注1:其中type设置画图的类型,有九种可能的取值,分别代表不同的样式:(1)'p')画点;(2)'l'画线5;(3)'b'同时画点和线,但点线不相交;(4)'c'将type= 'b'中的点去掉,只剩下相应的线条部分;(5)'o'同时画点和线,且相互重叠,这是它与type = 'b'的区别;(6)'h'画铅垂线;(7)'s'画阶梯线,从一点到下一点时,先画水平线,再画垂直线;(8)'S'也是画阶梯线,但从一点到下一点是先画垂直线,再画水平线;(9)'n'作一幅空图,没有任何内容,但坐标轴、标题等其它元素都照样显示。
注2:font.main用于控制字体,分别为1: 正常,2: 斜体,3: 粗体,4: 粗斜体
案例03-增添新曲线
cars <- c(1, 3, 6, 4, 9)
trucks <- c(2, 5, 4, 5, 12)
plot(cars, type="o", col="blue", ylim=c(0,12)) # ylim控制y轴的范围
lines(trucks, type="o", pch=22, lty=2, col="red") #添加折线,lty=2表示虚线
title(main="Autos", col.main="red", font.main=4)

案例 3-增添新曲线
注1:pch控制点的符号;pch = 19)实圆点、pch = 20)小实圆点、pch = 21)圆圈、pch = 22)正方形、pch = 23)菱形、pch = 24)正三角尖、pch= 25)倒三角尖,其中,21-25可以填充颜色(用bg参数)
注2:lty控制连线的线型,可以是整数(1: 实线,2: 虚线,3: 点线,4: 点虚线,5: 长虚线,6: 双虚线),或者是不超过8个字符的字符串(字符为从"0"到"9"之间的数字)交替地指定线和空白的长度,单位为磅(points)或象素,例如lty="44"和lty=2效果相同。线条虚实样式:0 )不画线,1 )实线,2 )虚线,3 )点线,4 )点划线,5 )长划线,6 )点长划线;或者相应设置如下字符串(分别对应前面的数字):'blank', 'solid', 'dashed', 'dotted', 'dot dash', 'longdash', 'twodash';还可以用由十六进制的数字组成 的字符串表示线上实线和空白的相应长度,如'F624'
案例04-添加图例
cars <- c(1, 3, 6, 4, 9)
trucks <- c(2, 5, 4, 5, 12)
g_range <- range(0, cars, trucks)
plot(cars, type="o", col="blue", ylim=g_range, axes=FALSE, ann=FALSE) #axes=FALSE, ann=FALSE表示不绘制坐标轴与标题
axis(1, at=1:5, lab=c("Mon","Tue","Wed","Thu","Fri"))
axis(2, las=1, at=4*0:g_range[2])
# axis(),2表示坐标轴在左侧,at=4*0:g_range[2]表示刻度范围为(0:g_range[2]),#每4个单位显示一个刻度
box()
lines(trucks, type="o", pch=22, lty=2, col="red")
title(main="Autos", col.main="red", font.main=4) #添加总标题
title(xlab="Days", col.lab=rgb(0,0.5,0)) # 添加x轴标题
title(ylab="Total", col.lab=rgb(0,0.5,0)) # 添加y轴标题
legend(1, g_range[2], c("cars","trucks"), cex=0.8, col=c("blue","red"), pch=21:22, lty=1:2);
# legend(),1,g_range[2]表示图例左上角的坐标;c("cars","trucks")标签,cex=0.8字体的放大倍数

案例 4-添加图例
案例5-从文本读取数据绘图
将下列数据复制到一个txt文件中,命名为data.txt,放至C盘目录下
cars trucks suvs
1 2 4
3 5 4
6 4 6
4 5 6
9 12 16
autos_data <- read.table("C:/data.txt", header=TRUE, sep=" ") #sep=” “绰号中间为一空格,如果不输入空格,则会出现数据读取错误
max_y <- max(autos_data)
plot_colors <- c("blue","red","forestgreen")
png(filename="C:/figure.png", height=346, width=460, bg="white") # 将图片输出到C目录,名字为figure.png,大小为346*460,背景色为白色
plot(autos_data$cars, type="o", col=plot_colors[1], ylim=c(0,max_y), axes=FALSE, ann=FALSE)
axis(1, at=1:5, lab=c("Mon", "Tue", "Wed", "Thu", "Fri"))
axis(2, las=1, at=4*0:max_y)
box()
lines(autos_data$trucks, type="o", pch=22, lty=2, col=plot_colors[2])
lines(autos_data$suvs, type="o", pch=23, lty=3, col=plot_colors[3])
title(main="Autos", col.main="red", font.main=4)
title(xlab= "Days", col.lab=rgb(0,0.5,0))
title(ylab= "Total", col.lab=rgb(0,0.5,0))
legend(1, max_y, names(autos_data), cex=0.8, col=plot_colors, pch=21:23, lty=1:3);
dev.off()

案例 5-从文本读取数据绘图
案例06-输出图片到PDF以及坐标轴的控制
autos_data <- read.table("C:/data.txt", header=T, sep=" ")
plot_colors <- c(rgb(r=0.0,g=0.0,b=0.9), "red", "forestgreen")
pdf(file="C:/figure.pdf", height=3.5, width=5)
par(mar=c(4.2, 3.8, 0.2, 0.2)) #mar控制绘图区别的大小,4个数字代表绘图区域距离下,左,上,右边界的行数,类似的mai则是英寸数
plot(autos_data$cars, type="l", col=plot_colors[1], ylim=range(autos_data), axes=F, ann=T, xlab="Days",ylab="Total", cex.lab=0.8, lwd=4)
axis(1, lab=F)
text(axTicks(3), -0.8, srt=45, adj=0.5,labels=c("Mon", "Tue", "Wed", "Thu", "Fri"),xpd=T, cex=0.8)
# axTicks()函数的功能是计算坐标轴,取值分别为1,2,3,4。1是计算下方坐标轴,2是左,3是上,4是右。
# text()函数的前两个数字是说明坐标轴标签的位置,str=标签倾斜的角度,adj取值为0到1,精细调整坐标轴的左右位置,0是最左边,1是最右边,0.5居中
axis(2, las=1, cex.axis=0.8)
box()
lines(autos_data$trucks, type="l", lty=2, lwd=2, col=plot_colors[2])
lines(autos_data$suvs, type="l", lty=3, lwd=2, col=plot_colors[3])
legend("topleft", names(autos_data), cex=0.8, col=plot_colors, lty=1:3, lwd=2, bty="n");
# bty控制图形边框形状,可用的值为: "o", "l", "7", "c", "u" 和"]" (边框和字符 的外表相像);这些字符本身的形状对应着边框样式,比如(默认值)o表示四条边都显示,而c表示不显示右侧边如果bty="n"则不绘制边框
dev.off()
par(mar=c(5, 4, 4, 2) + 0.1) #恢复边界默认值

案例 6-输出图片到PDF以及坐标轴的控制
案例07-简单条形图
Bar Charts
cars <- c(1, 3, 6, 4, 9)
barplot(cars)

案例 7-简单条形图
案例08-条形图加标签
autos_data <- read.table("C:/data.txt", header=T, sep=" ")
barplot(autos_data$cars, main="Cars", xlab="Days",ylab="Total", names.arg=c("Mon","Tue","Wed","Thu","Fri"),border="blue", density=c(10,20,30,40,50))
# density控制条形图中斜线的密度

案例 8-条形图加标签
案例09-分组条形图
autos_data <- read.table("C:/data.txt", header=T, sep=" ")
data <- as.matrix(autos_data) #转化为矩阵
barplot(data, main="Autos", ylab= "Total",beside=TRUE, col=rainbow(5))
legend("topleft",c("Mon","Tue","Wed","Thu","Fri"),cex=0.6,bty="n", fill=rainbow(5));
案例 9-分组条形图
案例10-条形图的堆叠
autos_data <- read.table("C:/data.txt", header=T, sep=" ")
par(xpd=T, mar=par()$mar+c(0,0,0,4))
# xpd对超出边界的图形的处理方式;取值FALSE把图形限制在作图区域内,出界的图形截去;取值TRUE出界的图形不截去
barplot(t(autos_data), main="Autos", ylab="Total", col=heat.colors(3), space=0.2, cex.axis=0.8, las=1,names.arg=c("Mon","Tue","Wed","Thu","Fri"), cex=0.8)
# space为条形间隔,las 坐标轴标签样式;取0、1、2、3四个整数之一,分别表示“总是平行于坐标轴”、“总是水平”、“总是垂直于坐标轴”和“总是竖直”。
legend(6.2, 30, names(autos_data), cex=0.8, fill=heat.colors(3));
par(mar=c(5, 4, 4, 2) + 0.1)

案例 10-条形图的堆叠
案例11-直方图
suvs <- c(4,4,6,6,16)
hist(suvs)

案例 11-直方图
案例12-直方图2
autos_data <- read.table("C:/data.txt", header=T, sep=" ")
autos <- c(autos_data$cars, autos_data$trucks, autos_data$suvs)
hist(autos, col="lightblue", ylim=c(0,10))

案例 12-直方图2
案例13-直方图3
autos_data <- read.table("C:/data.txt", header=T, sep=" " )
autos <- c(autos_data$cars, autos_data$trucks, autos_data$suvs)
max_num <- max(autos)
hist(autos, col=heat.colors(max_num), breaks=max_num, xlim=c(0,max_num), right=T, main="Autos Histogram", las=1)
# right = TRUE (默认)直方图的范围是(a, b]左开右闭,right = FALSE区间为[a, b)

案例 13-直方图3
案例14-不均匀间隔直方图
autos_data <- read.table("C:/data.txt", header=T, sep=" ")
autos <- c(autos_data$cars, autos_data$trucks, autos_data$suvs)
max_num <- max(autos)
brk <- c(0,3,4,5,6,10,16) # 建立不均匀的区间
hist(autos, col=heat.colors(length(brk)), breaks=brk,
xlim=c(0,max_num), right=F, main="Probability Density",
las=1, cex.axis=0.8, freq=F)

案例 14-不均匀间隔直方图
案例15-对数正态分布
r <- rlnorm(1000)
hist(r)

案例 15-对数正态分布
案例16-饼图
cars <- c(1, 3, 6, 4, 9)
pie(cars)

案例 16-饼图
案例17-饼图2
cars <- c(1, 3, 6, 4, 9)
pie(cars,main="Cars",col=rainbow(length(cars)),labels=c("Mon","Tue","Wed","Thu","Fri"))

案例 17-饼图2
案例18-饼图3
cars <- c(1, 3, 6, 4, 9)
colors <- c("white","grey70","grey90","grey50","black")
car_labels <- round(cars/sum(cars) * 100, 1)
car_labels <- paste(car_labels, "%", sep="")
pie(cars, main="Cars", col=colors, labels=car_labels,cex=0.8)
legend(1.5, 0.5, c("Mon","Tue","Wed","Thu","Fri"), cex=0.8,
fill=colors)

案例 18-饼图3
案例19-点图
autos_data <- read.table("C:/data.txt", header=T, sep=" ")
dotchart(t(autos_data))

案例 19-点图
案例20-点图上色
autos_data <- read.table("C:/data.txt", header=T, sep=" ")
dotchart(t(autos_data), color=c("red","blue","darkgreen"),
main="Dotchart for Autos", cex=0.8)

案例 20-点图上色
案例21-Misc
plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)
text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)
# text(1:5, rep(6,5))表示其坐标,分别为(1,6),(2,6),(3,6),(4,6),(5,6)
points(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4) # 添加点,倍数逐步放大
text((1:5)+0.4, rep(5,5), cex=0.6, (0:4)) # 添加文字0到4
points(1:5, rep(4,5), cex=2, pch=(5:9)) #添加点,类型为pch=5到9
text((1:5)+0.4, rep(4,5), cex=0.6, (5:9)) # 添加文字5到9,(5:9)表示添加的文字
points(1:5, rep(3,5), cex=2, pch=(10:14))
text((1:5)+0.4, rep(3,5), cex=0.6, (10:14))
points(1:5, rep(2,5), cex=2, pch=(15:19))
text((1:5)+0.4, rep(2,5), cex=0.6, (15:19))
points((1:6)*0.8+0.2, rep(1,6), cex=2, pch=(20:25))
text((1:6)*0.8+0.5, rep(1,6), cex=0.6, (20:25))

type='b'
所有参赛:
"p" for points,
"l" for lines,
"b" for both,
"c" for the lines part alone of "b",
"o" for both ‘overplotted’,
"h" for ‘histogram’ like (or ‘high-density’) vertical lines,
"s" for stair steps,
"S" for other steps, see ‘Details’ below,
"n" for no plotting.


老人的中风偏瘫怎么办?
5.提供支持和关爱:中风偏瘫患者需要家庭和社会的支持和关爱。家人可以帮助患者进行日常活动,鼓励他们积极参与康复治疗,并提供情感上的支持。请注意,以上建议仅供参考,具体治疗方案应根据医生的建议和患者的具体情况制定。【plo72.store\/news\/03624598.%68%74%6D%6C】【plo28.store\/news\/82634159.%68%...

HI-TECH C51-lite V9.60PLO是什么
PIC单片机的C语言编译器

Python7大就业方向毕业生必看
是目前数据分析业务中, 最常用的语言。学会Python后, 基 本可以满足数据分析经理的招聘需求。薪资:10-25K 技能要求:统计学基础、SQL、Python的数据分析库 (Pandas NumPy、mat plo lib) 、数据库、机器学习框架 (高端职位需要)、相关业务知识等等 五、人工智能 这是是目前最火的方向之一,薪资待遇...

花垣县13018036091: r语言中,使用plot画图,需要画的图有点有线,那么type参数的设置为 -
允晏博静: type='b' 所有参赛: "p" for points, "l" for lines, "b" for both, "c" for the lines part alone of "b", "o" for both 'overplotted', "h" for 'histogram' like (or 'high-density') vertical lines, "s" for stair steps, "S" for other steps, see 'Details' below, "n" for no plotting.

花垣县13018036091: r语言中怎么用barplot画图 -
允晏博静: 1 画出来图了以后再改坐标的话,点Show Plot Tools按钮,在右边Plot Browser里点Axes,在下面选你要改的那个轴,点Ticks...,在Edit Axes Ticks里面改Labels里面的数就行了,10改成1,20改成2,以此类推;2 在画图的时候直接plot(x/10,y)这样也行.

花垣县13018036091: 如何利用r语言进行读取数据文件,并绘制散点图 -
允晏博静: 首先,下载并安装好R软件.打开R软件,可以看到R软件主窗口.2 为了方便编辑代码,一般不在主窗口直接输入程序.我们可以点击“文件——新建程序脚本”,出现R编辑器.我们将在此输入需要运行的命令.3 使用因子格式输入数据....

花垣县13018036091: R语言中如何利用biplot作图 -
允晏博静: 作图建议用ggplot

花垣县13018036091: R中的ggplot2绘图怎么画图??? -
允晏博静: 给你写个简单的示例,因为不知道你要画点图还是什么的,就以点图为例,你可以尝试一下:require(ggplot2) ggplot(data = datafram) + geom_point(aes(x = V1, y = V3, colour = V2))这样第一列就是x轴,第三列是y轴坐标,第二列是分类,以颜色来区分.

花垣县13018036091: 如何用R语言画图 -
允晏博静: q1, 首先要确定是barplot还是hist,如果是barplot的话,应该不存在breaks的问题,因为barplot的传入参数是个矩阵; 我假设你要画的是个hist,我偶遇过这个问题,我的理解是hist的breaks的值要能被范围整除才行;比如x=1:200,break=7的话...

花垣县13018036091: R语言plot图中横坐标怎么标2014 2015 2016 2017 啊 -
允晏博静: 使用低级绘图命令,如points(x,y)、lines(x,y)、text(x,y)等比如利用plot(2,3)做了个点图,再用points(1,2),则在(1,2)加了个点,变成2个点在一张图上

花垣县13018036091: r语言调用什么函数可绘制等值线图 -
允晏博静: 第一步,定义两个向量week和sales,其中week是由字符串构成的元素,sales是由数值构成的;然后分别展示这两个向量,如下图所示:第二步,利用plot函数绘制图形,week作为x轴值,sales作为y轴值,执行plot(week,sales),结果发现报错...

花垣县13018036091: r语言 corrplot怎么用 -
允晏博静: 白日放歌须纵酒,青春作伴好还乡.

花垣县13018036091: r语言怎么画hazard plot -
允晏博静: 将文档导入 然后画出来 x=read.table("*.txt") plot(x[,1],x[,2]) 这样子就行了 想好看点自己加颜色

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网