Đây có lẽ không phải là cách thanh lịch nhất để làm điều này nhưng nó hoạt động (từ đầu và không sử dụng ternaryplot
mặc dù: Tôi không thể tìm ra cách để làm điều đó).
a<- c (0.1, 0.5, 0.5, 0.6, 0.2, 0, 0, 0.004166667, 0.45)
b<- c (0.75,0.5,0,0.1,0.2,0.951612903,0.918103448,0.7875,0.45)
c<- c (0.15,0,0.5,0.3,0.6,0.048387097,0.081896552,0.208333333,0.1)
d<- c (500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04)
df<- data.frame (a, b, c)
# First create the limit of the ternary plot:
plot(NA,NA,xlim=c(0,1),ylim=c(0,sqrt(3)/2),asp=1,bty="n",axes=F,xlab="",ylab="")
segments(0,0,0.5,sqrt(3)/2)
segments(0.5,sqrt(3)/2,1,0)
segments(1,0,0,0)
text(0.5,(sqrt(3)/2),"c", pos=3)
text(0,0,"a", pos=1)
text(1,0,"b", pos=1)
# The biggest difficulty in the making of a ternary plot is to transform triangular coordinates into cartesian coordinates, here is a small function to do so:
tern2cart <- function(coord){
coord[1]->x
coord[2]->y
coord[3]->z
x+y+z -> tot
x/tot -> x # First normalize the values of x, y and z
y/tot -> y
z/tot -> z
(2*y + z)/(2*(x+y+z)) -> x1 # Then transform into cartesian coordinates
sqrt(3)*z/(2*(x+y+z)) -> y1
return(c(x1,y1))
}
# Apply this equation to each set of coordinates
t(apply(df,1,tern2cart)) -> tern
# Intrapolate the value to create the contour plot
resolution <- 0.001
require(akima)
interp(tern[,1],tern[,2],z=d, xo=seq(0,1,by=resolution), yo=seq(0,1,by=resolution)) -> tern.grid
# And then plot:
image(tern.grid,breaks=c(-1000,0,500,1000,1500,2000,3000),col=rev(heat.colors(6)),add=T)
contour(tern.grid,levels=c(-1000,0,500,1000,1500,2000,3000),add=T)
points(tern,pch=19)
Chào mừng bạn đến StackOverflow. Có lẽ bạn nên gắn thẻ câu hỏi của mình bằng ngôn ngữ bạn đang viết nó, hoặc ít nhất là đề cập đến ngôn ngữ trong câu hỏi của bạn. Để làm như vậy, bạn có thể sử dụng nút 'chỉnh sửa'. – ninjagecko
xin lỗi, tôi đang sử dụng mã [r] – FraNut
bắt đầu bằng 'RSiteSearch (" đường bao bậc ba ")' và xem điều đó có hữu ích không? Ngoài ra 'thư viện (" sos "); findFn ("đường viền ternary") ' –