Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r中的阴影三重波_R_Plot_Ggplot2_Triplot - Fatal编程技术网

r中的阴影三重波

r中的阴影三重波,r,plot,ggplot2,triplot,R,Plot,Ggplot2,Triplot,我可以用klaR包算出triplot require(klaR) triplot(label = c("1, 2 or 3", "4 or 5", "6"), main = "die rolls: probabilities", pch = 17) 我想画一个带阴影的三点图,这样我就可以显示特定点属于哪个类 是否有任何包(发达或欠发达)可以这样做?或者我们可以调整可用的包来实现这一点 编辑:针对以下答案: xpoint <- matrix(c(0, 0, 10, 0

我可以用klaR包算出triplot

   require(klaR)

triplot(label = c("1, 2 or 3", "4 or 5", "6"), 
    main = "die rolls: probabilities", pch = 17)
我想画一个带阴影的三点图,这样我就可以显示特定点属于哪个类

是否有任何包(发达或欠发达)可以这样做?或者我们可以调整可用的包来实现这一点

编辑:针对以下答案:

xpoint <- matrix(c(0, 0, 10,  0, 10, 0, 10,0,0, 10,10, 0, 10,0,10, 0,0, 10, 0,10,10, 10,10,10), ncol =3, byrow= TRUE)
xp <- t(apply(xpoint,1,tern2cart))
points(xp[,1], y = xp[,2], type = "p", col = "green", pch = "*", cex = 4)
text(xp[,1]-0.01,xp[,2]-0.01)
> xpoint 


       [,1] [,2] [,3]
    [1,]    0    0   10
    [2,]    0   10    0
    [3,]   10    0    0
    [4,]   10   10    0
    [5,]   10    0   10
    [6,]    0    0   10
    [7,]    0   10   10
    [8,]   10   10   10
xpoint因此(关于我给出的答案),我无法使用
triplot
为您提供答案(因为我不知道该函数用于绘制图表的引用),但这里有一个从头开始的解决方案:

#First draw the empty ternary diagram:
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,0,labels="1, 2 or 3",pos=1)
text(1,0,labels="6",pos=1)
text(0.5,sqrt(3)/2,labels="4 or 5",pos=3)

#The following function is for transforming ternary coordinates into cartesian coordinates:
tern2cart <- function(coord){
    coord[1]->x
    coord[2]->y
    coord[3]->z
    x+y+z->tot
    x/tot -> x
    y/tot -> y
    z/tot -> z
    (2*y + z)/(2*(x+y+z)) -> x1
    sqrt(3)*z/(2*(x+y+z)) -> y1
    return(c(x1,y1))
    }

#Here are your zones:
green.zone<-matrix(c(0,0,100,40,0,60,0,40,60,0,0,100),nrow=4,byrow=TRUE)
blue.zone<-matrix(c(30,10,60,30,40,30,0,70,30,0,40,60,30,10,60),nrow=5,byrow=TRUE)
purple.zone<-matrix(c(90,0,10,100,0,0,30,70,0,30,40,30,50,40,10,90,0,10),nrow=6,byrow=TRUE)
red.zone<-matrix(c(30,40,30,30,70,0,0,100,0,0,70,30,30,40,30),nrow=5,byrow=TRUE)
yellow.zone<-matrix(c(90,0,10,40,0,60,30,10,60,30,40,30,50,40,10,90,0,10),nrow=6,byrow=TRUE)

#Then transformed into cartesian coordinates:
t(apply(green.zone,1,tern2cart))->green
t(apply(blue.zone,1,tern2cart))->blue
t(apply(purple.zone,1,tern2cart))->purple
t(apply(red.zone,1,tern2cart))->red
t(apply(yellow.zone,1,tern2cart))->yellow

#And plotted:
polygon(green,col="green",border=NULL)
polygon(blue,col="blue",border=NULL)
polygon(purple,col="purple",border=NULL)
polygon(red,col="red",border=NULL)
polygon(yellow,col="yellow",border=NULL)

#And finally the grid:
a<-seq(0.9,0.1, by=-0.1)
b<-rep(0,9)
c<-seq(0.1,0.9,by=0.1)
grid<-data.frame(x=c(a, b, c, a, c, b),y=c(b, c, a, c, b, a),z=c(c, a, b, b, a, c))
t(apply(grid,1,tern2cart)) -> grid.tern
cbind(grid.tern[1:27,],grid.tern[28:54,])->grid
apply(grid,1,function(x){segments(x0=x[1],y0=x[2],x1=x[3],y1=x[4],lty=2,col="grey80")})

并在图表上绘制数据

df<-data.frame('1, 2 or 3'=c(10,33.3,50,100), '6'=c(0,33.3,50,0), '4 or 5'=c(90,33.3,0,0))
df
  X1..2.or.3   X6 X4.or.5
1       10.0  0.0    90.0
2       33.3 33.3    33.3
3       50.0 50.0     0.0
4      100.0  0.0     0.0

t(apply(df, 1, tern2cart)) -> df.tern
points(df.tern, pch="*", cex=3)
df.tern
点(df.tern,pch=“*”,cex=3)

使用我最近通过CRAN发布的软件包,可以获得以下信息:

使用以下代码可以实现上述结果:

g <- data.frame(x=c(1,.6,.6),
                y=c(0,.4,0),
                z=c(0,0,.4),         Series="Green")
y <- data.frame(x=c(.6,.1,.1,.3,.6),
                y=c(.4,.9,.5,.3,.3),
                z=c( 0, 0,.4,.4,.1), Series="Yellow")
b <- data.frame(x=c(.6,.3,.3,.6),
                y=c(.3,.3, 0, 0),
                z=c(.1,.4,.7,.4),    Series="Blue")
r <- data.frame(x=c(.3, 0,0,.3),
                y=c(.3,.3,0, 0),
                z=c(.4,.7,1,.7),     Series="Red")
p <- data.frame(x=c(.1,0, 0,.3,.1),
                y=c(.9,1,.3,.3,.5),
                z=c( 0,0,.7,.4,.4),  Series="Purple")
DATA=rbind(g,y,b,r,p)
ggtern(data=DATA,aes(x,y,z)) + 
geom_polygon(aes(fill=Series),alpha=.5,color="black",size=0.25) +
  scale_fill_manual(values=as.character(unique(DATA$Series))) +
  theme(legend.position=c(0,1),legend.justification=c(0,1)) + 
  labs(fill="Region",title="Sample Filled Regions")

如果ggplot2有这样的geom_triplot(作为3D plot的替代品,目前还没有),g就不酷了……谢谢大家!我没有具体说明包,您的解决方案是可压缩的解决方案,而不是包中的解决方案…我对cordinates有疑问…因为多边形似乎以0-100的比例播放,但网格以0-1的比例播放…如果我需要添加点图-散点图(如我的示例图中隐藏的三角形点)如何,训练…再次感谢您在编辑中的点对我来说似乎很好:坐标(0,10,0)在三元图上等于(0100,0)或(0,1,0)或任何(0,n,0)
g <- data.frame(x=c(1,.6,.6),
                y=c(0,.4,0),
                z=c(0,0,.4),         Series="Green")
y <- data.frame(x=c(.6,.1,.1,.3,.6),
                y=c(.4,.9,.5,.3,.3),
                z=c( 0, 0,.4,.4,.1), Series="Yellow")
b <- data.frame(x=c(.6,.3,.3,.6),
                y=c(.3,.3, 0, 0),
                z=c(.1,.4,.7,.4),    Series="Blue")
r <- data.frame(x=c(.3, 0,0,.3),
                y=c(.3,.3,0, 0),
                z=c(.4,.7,1,.7),     Series="Red")
p <- data.frame(x=c(.1,0, 0,.3,.1),
                y=c(.9,1,.3,.3,.5),
                z=c( 0,0,.7,.4,.4),  Series="Purple")
DATA=rbind(g,y,b,r,p)
ggtern(data=DATA,aes(x,y,z)) + 
geom_polygon(aes(fill=Series),alpha=.5,color="black",size=0.25) +
  scale_fill_manual(values=as.character(unique(DATA$Series))) +
  theme(legend.position=c(0,1),legend.justification=c(0,1)) + 
  labs(fill="Region",title="Sample Filled Regions")