R语言如何画竖线、横线、添加标签以及画固定长度的线段

前言

注:在ggplot2中不能使用abline,segments,在基础绘图中不能使用annotate

1.基础绘图:

画横线: abline(h=y0)

画竖线:abline(v=x0)

添加固定长度线段:segments(x0=, y0=, x1=, y1=, ...)

添加文字标签:text(x=x0, y=y0, label=" ")

例子:(本来要上传图片的,但是老是上传失败QAQ)

m <- plot(1:10,1:10,"l")+
 abline(v=3,lwd=2,lty=2,col="brown")+ #lwd设置线的宽度,lty表示虚线,其后数字表示虚线类型
 abline(h=3,lwd=2,lty=3)
m+text(x=4,y=4,label="*",col="red")+text(x=5,y=4,label="A(4,4)")
m+segments(x0=2,y0=6,x1=8,y1=6) # y=6,x轴从2-8的线段

2.ggplot2绘图:

画横线:geom_hline(yintercept= )

画竖线:geom_vline(xintercept=)

加标注:annotate("text",x=,y=,label="")

例子:用women数据集举例

f <- ggplot(women,aes(height,weight))+geom_point()
f+geom_vline(xintercept = 64,lty=3,lwd=1,col="blue")+
 geom_hline(yintercept = 123)
f+annotate("text",x=60,y=150,label="*",size=5,col="red")

补充:R语言——一张图上画多条线

第一种:

plot(M[1:20],type="o",pch=20,col="red")
par(new = TRUE)
plot(N[1:20],type="o",pch=17,col="blue")

中间加上这一行par(new = TRUE)就好了。

第二种:

选择用lines

plot(M[1:20],type="o",pch=20,col="red")
points(N[1:20],pch=17,col="blue")
lines(N,col="Blue",lty=2)

或者是:

plot(M[1:20],type="o",pch=20,col="red")
lines(N[1:20],pch=17,type="o",col="Blue",lty=2)

另外

dev.new()

是指重新开个窗口画图。

总结

作者:Griffy650原文地址:https://blog.csdn.net/weixin_56198196/article/details/123307026

%s 个评论

要回复文章请先登录注册