简体   繁体   English

R中的双环图

[英]plot for double loop in R

My question is about the for loop in combination with the plot function in R. I want to plot multiple points in one plot. 我的问题是将for循环与R中的plot函数结合使用。我想在一个图中绘制多个点。 I do not know what is wrong with my functions. 我不知道我的功能出了什么问题。 Any help please? 有什么帮助吗?

DATA
 a x y  z
149 1 1  0
153 1 1 10
160 1 1 10
149 1 2  0
153 1 2  0
160 1 2 10
149 2 1  0
153 2 1  0
160 2 1  5
149 2 2  0
153 2 2  0
160 2 2  5

PCH=0;
plot(c(142,169),c(0,11),type="n")
for(i in unique(DATA$x)) {
  for(j in unique(DATA$y)) {
    PCH=PCH+1
    select <- DATA[i,j]
    X = DATA[select,"a"]; 
    Y = DATA[select,"z"]
    points(X,Y,pch=PCH)
  }  
}

Does this by chance do what you want to achieve? 这是不是偶然地实现了您想要实现的目标?

plot(z~a,data=DATA,
         pch=as.numeric(interaction(x,y)),
         xlim=c(142,169),ylim=c(0,11))

Your selection is wrong. 您的选择是错误的。 Try the following code: 尝试以下代码:

PCH <- 0
plot(c(142,169), c(0,11), type="n")
for(i in unique(DATA$x)) {
  for(j in unique(DATA$y)) {
    PCH <- PCH+1
    select <- DATA$x == i & DATA$y == j
    X <- DATA[select,"a"] 
    Y <- DATA[select,"z"]
    points(X,Y,pch=PCH)
  }  
}

Note that it is better style to use <- instead of = , as = is also used in other purposes where it has another syntactical meaning. 请注意,使用<-代替=会是更好的样式,因为=还会在具有其他语法含义的其他目的中使用。 Forthermore, you do not need ; 此外,您不需要; at the end of the line in R. 在R行的末尾

your X and Y values are off.. you don't need the select statement 您的X和Y值已关闭..您不需要select语句

  plot(c(142,169),c(0,11),type="n")
  for(i in unique(DATA$x)) {
    for(j in unique(DATA$y)) {
      PCH=PCH+1
      X = DATA[(DATA$x==i) & (DATA$y==j),"a"]; 
      Y = DATA[(DATA$x==i) & (DATA$y==j),"z"]
     (points(X,Y,pch=PCH))
    }  
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM