我试图通过创建多边形并将其填充来从图形的顶部(y = 5)着色到原始绘图中的线。我正在以某种方式搞乱点生成。有人可以在这里解释出什么问题吗? (不是要填写三角形)
half_instances<-c(0,5,2)
Ts<-c(1,2,3)
xpairs<-c(Ts, rep(5,length(half_instances)))
ypairs<-c(Ts,half_instances)
xpairs #1 2 3 5 5 5
ypairs #0 5 2 1 2 3
plot(Ts,half_instances,type="l")
polygon(xpairs,ypairs)
意外输出:
答案 0 :(得分:1)
因为在坐标X = 5处有点,所以如果要查看整个多边形,则需要修改xlim:
half_instances<-c(0,5,2)
Ts<-c(1,2,3)
xpairs<-c(Ts, rep(5,length(half_instances)))
ypairs<-c(half_instances,Ts)
xpairs #1 2 3 5 5 5
ypairs #0 5 2 1 2 3
plot(Ts,half_instances,type="l",xlim=c(1,5))
polygon(xpairs,ypairs)
答案 1 :(得分:1)
答案 2 :(得分:1)
您将x和y值混合在一起,需要将5放入y坐标的向量中:
half_instances<-c(0,5,2)
Ts<-c(1,2,3)
xpairs <- c(Ts, rev(Ts))
xpairs # 1 2 3 3 2 1 = original x-values from left to right for the bottom half, then go back from right to left by using the reverse of the original x-values
ypairs <- c(half_instances, rep(5, length(half_instances)))
ypairs # 0 5 2 5 5 5 = original y-values for bottom half, then fill up with 5's tor the top half
plot(Ts, half_instances,type="l")
polygon(xpairs, ypairs, col="red")