如何在没有回路的情况下创建以R为基数的组的线图?

如何在没有回路的情况下创建以R为基数的组的线图?,r,R,我需要使用以下数据创建一个包含组的简单线图: test = data.frame(x = rep(1:3, each = 2), group = rep(c("Group 1","Group 2"),3), groupcd= rep(c(1,2),3), y= c(22,8,11,4,7,5) ) 我可以很容易地使用GGPLOT: librar

我需要使用以下数据创建一个包含组的简单线图:

test = data.frame(x =  rep(1:3, each = 2),
                  group =  rep(c("Group 1","Group 2"),3),
                  groupcd= rep(c(1,2),3),
                  y=   c(22,8,11,4,7,5)
                  )
我可以很容易地使用GGPLOT:

library(ggplot2)
    #GGPLOT
    qplot(x=x, y=y, 
           data=test, 
           colour=group, 
           main="GGPLOT line plot with groups") +
      geom_line()

我也可以使用网格:

library(lattice)
xyplot(y~x,
       type="b",
       group=group,
       data=test,
       main="TRELLIS line plot with groups",
       auto.key =list(
         lines = TRUE)
       )

然而,我现在有点不愿意使用GGPLOT或Grillis。我希望能够使用Base R创建此图。使此绘图在Base R中工作的唯一方法是使用for循环:

# set up empty plot
plot(test$y ~test$x,  ylab="y", xlab="x", type="n", main="Base R line plot with groups")
colors<-c("red","blue")
#plot each group in the for loop
number_of_groups <- as.numeric(max(unique(test$groupcd))) #calculate number of groups
for (i in 1:number_of_groups) 
{
  temp <- subset(test,  groupcd==i )
  lines(temp$x, temp$y, col=colors[i])  
  points(temp$x, temp$y, col=colors[i])  
}
legend("top", legend=unique(test$group), text.col =colors  )
#设置空打印
绘图(test$y~test$x,ylab=“y”,xlab=“x”,type=“n”,main=“带组的基线R线绘图”)

颜色像这样的东西作为工作的基础怎么样:

test = data.frame(x = rep(1:3, each = 2),
                  group = rep(c("Group 1", "Group 2"), 3),
                  group_cd = rep(c(1, 2), 3),
                  y = c(22, 8, 11, 4, 7, 5))

xvals <- split(test$x, test$group)
yvals <- split(test$y, test$group)

plot(1:max(unlist(xvals)), ylim = c(0, max(unlist(yvals))), type = "n")
# thanks to @BenBolker for refining this next key line
mapply(lines, xvals, yvals, col = c("red", "blue"), pch = 1:2, type = "o")
test=data.frame(x=rep(1:3,每个=2),
组=代表(c组(“第1组”、“第2组”)、第3组),
组cd=rep(c(1,2,3),
y=c(22,8,11,4,7,5))

xVAL使用MATPLOT的不同方法:

library(reshape)
test = data.frame(x =  rep(1:3, each = 2),
                  group =  rep(c("Group 1","Group 2"),3),
                  groupcd= rep(c(1,2),3),
                  y=   c(22,8,11,4,7,5)
                  )
colors<-c("red","blue")

#Transform  data to wide format 
test_transposed<-reshape(test, 
                         idvar='x',
                         drop="group",
                         timevar="groupcd", 
                         direction="wide")
colors<-c("red","blue")

#drop x column
test_transposed$x<-NULL

matplot(test_transposed, 
        type = "b",
        ylab="y",
        col=colors,
        main="MATPLOT with groups",
        pch = 1:2)

legend("top", 
       legend=unique(test$group), 
       lty=1:2, 
       col=colors,
       pch=1:2  )
库(重塑)
测试=数据帧(x=重复(1:3,每个=2),
组=代表(c组(“第1组”、“第2组”)、第3组),
groupcd=rep(c(1,2,3),
y=c(22,8,11,4,7,5)
)

颜色我也在想这个。我的面板数据没有完全覆盖x轴(年),因此类似矩阵的解决方案可能会变得复杂。相反,我和

mytest = data.frame(
              x =  rep(1:3, each = 2),
              groupcd= rep(c(1,2),3),
              y=   c(22,8,11,4,7,5)
              )
mytest = rbind(mytest,c(2,3,15),c(3,3,17))

plottables <- split(mytest,mytest$groupcd)
plot(y~x,dat=plottables[[1]],type="l",xlim=range(mytest$x),ylim=range(mytest$y))
lapply(plottables,function(z)points(y~x,dat=z,type="l"))
mytest=data.frame(
x=rep(1:3,每个=2),
groupcd=rep(c(1,2,3),
y=c(22,8,11,4,7,5)
)
mytest=rbind(mytest,c(2,3,15),c(3,3,17))

plottables将数据转换为宽格式,并使用
matplot
…非常感谢!我甚至不知道matplot——这非常有用。这是一个非常好的解决方案,似乎是迄今为止最好的选择。我真的很惊讶,用情节来分组是不容易的。谢谢,我认为你应该提交你的
matplot
解决方案作为答案,而不是对你的问题进行编辑(尽管根据你的声誉,你可能需要等待一段时间——我不知道)。我想我可以相当自信地说,
base::plot
中没有简单的组选项。关于上面代码的几个其他想法:(1)我认为可以使用
子集(test_transposed,select=-x)
删除
x
列;(2) 您可能希望在您的
legend
语句中使用
lty=1:2、col=colors、pch=1:2
。这是一种非常好的方法。然而,它仍然不是循环方法的变体吗?i、 e.没有“分组”变量,如网格图中的。我正要提交matplot解决方案作为答案。我对R是个新手,无法决定哪种方法更好。谢谢,但这可能是过早的赞扬。我不知道如何在
mapply
语句中传递颜色向量,使每一行看起来不一样。这肯定是可能的,但它可能会使这个“简单”的解决方案再次变得复杂。这是一个非常可扩展的解决方案,例如
mapply(lines,xvals,yvals,col=c(“red”,“blue”),lty=1:2,pch=1:2,MoreArgs=list(type=“b”)
——除了
MoreArgs
@BenBolker中的参数外,所有参数都被矢量化了——谢谢你指出这一点。我已经被
MoreArgs
组件迷住了,把自己绑在了一起。