R语言-散点图、折线图

2020-06-07  本文已影响0人  科研小徐

散点图

a=read.table("BMI.txt",sep="\t",header = T,row.names = 1)
a
plot(a$weight,a$height,#设定x、y轴
     type="p",#     p:点      l:直线   b:点和直线
     main="weight vs height",#标题
     xlab = "weight(kg)",#x轴名称
     ylab = "height(cm)",#y轴名称
     xlim = c(55,75),#X轴范围
     ylim = c(160,180),#y轴范围
     col="red",#颜色
     pch=2)#形状
#可以改变pch、col、type
pch参数
图示
################################################
#排序后画折线图
################################################
#数据排序
##得到序号
index=order(a$weight,decreasing=F)
index
#按序号因子排序取数据
data=a[index,]
data
plot(data$weight, data$height,# x,y
     type="b",
     main="weight vs height", # 标题
     xlab="weight(kg)", #X轴名称
     ylab="height(cm)", #Y轴名称
     ylim=c(160,180), #Y轴范围
     xlim=c(55,75), #X轴范围
     col="red", #颜色
     pch=19) #形状
#####################################################
#加折线
#####################################################
male=data[data$gender=="male",]
female=data[data$gender=="female",]
plot(female$weight, female$height,# x,y
     type="n",#n为空图
     main="weight vs height", # 标题
     xlab="weight(kg)", #X轴名称
     ylab="height(cm)", #Y轴名称
     ylim=c(160,180), #Y轴范围
     xlim=c(55,75), #X轴范围
     col="red", #颜色
     pch=19) #形状
#加折线
lines(male$weight, male$height,col="blue",type="b")
lines(female$weight, female$height,col="red",type="b")
##############################################
#指定颜色和形状,分组
#############################################
color=ifelse(data$gender=="male","blue","red")
shape=ifelse(data$gender=="male",19,21)
plot(data$weight, data$height,# x,y
     type="b",
     main="weight vs height", # 标题
     xlab="weight(kg)", #X轴名称
     ylab="height(cm)", #Y轴名称
     ylim=c(160,180), #Y轴范围
     xlim=c(55,75), #X轴范围
     col=color, #颜色
     pch=shape) #形状
#加注释
legend("topleft",legend=c("male","female"),col=c("blue","red"),pch=c(19,21))#先指定位置。指定分组,颜色,形状。一一对应
############################################
#加文字
###########################################
text(58,166,"Cindy")#指定x、y坐标
##########################################
#加直线
#########################################
abline(v=65,col="red",lty=3,lwd=3)
abline(h=170,col="green",lty=4,lwd=2)
#v垂直线。h水平线。lty线的样式。lwd宽度
#########################################
#加线性拟合直线
#########################################
result=lm(height~weight,data)
summary(result)
plot(data$weight, data$height,# x,y
     type="p",
     main="weight vs height", # 标题
     xlab="weight(kg)", #X轴名称
     ylab="height(cm)", #Y轴名称
     ylim=c(160,180), #Y轴范围
     xlim=c(55,75), #X轴范围
     col="red", #颜色
     pch=19)
abline(result,col="black")
text(60,178,"pvalue=0.0122\nR-squared:0.7815")
#########################################
#保存绘图
########################################
pdf(file="line2.pdf",width=10,height=7)
plot(data$weight, data$height,# x,y
     type="p",
     main="weight vs height", # 标题
     xlab="weight(kg)", #X轴名称
     ylab="height(cm)", #Y轴名称
     ylim=c(160,180), #Y轴范围
     xlim=c(55,75), #X轴范围
     col="red", #颜色
     pch=19)
abline(result,col="black")
text(60,178,"pvalue=0.0122\nR-squared:0.7815")
dev.off()
#输出后要结束。先生成的pdf为画板,无内容
上一篇 下一篇

猜你喜欢

热点阅读