猿问
下载APP

R语言绘图时,如何指定背景参数为蓝色?

R语言绘图时,如何指定背景参数为蓝色?


牧羊人nacy
浏览 508回答 1
1回答

米脂

原贴转自生物统计家园,原贴只贴出了代码,没有画图,这里运行了一下代码,复习了一下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 suvs1 2 43 5 46 4 64 5 69 12 16autos_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 Chartscars <- 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-直方图2autos_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-直方图3autos_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-饼图2cars <- c(1, 3, 6, 4, 9)pie(cars,main="Cars",col=rainbow(length(cars)),labels=c("Mon","Tue","Wed","Thu","Fri"))案例 17-饼图2案例18-饼图3cars <- 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-Miscplot(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到4points(1:5, rep(4,5), cex=2, pch=(5:9)) #添加点,类型为pch=5到9text((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))
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答