13

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

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()

在此处输入图像描述

我也可以用 TRELLIS 做到这一点:

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

在此处输入图像描述

但是,我现在有点不愿意使用 GGPLOT 或 TRELLIS。我希望能够使用 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  )

在此处输入图像描述

这种方法似乎相当复杂。在base R中有没有更简单的方法来做到这一点?基本 R 绘图功能中是否有组选项?非常感谢。

4

3 回答 3

10

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

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")

结果:

在此处输入图像描述

于 2012-05-10T00:22:03.103 回答
3

使用 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  )

在此处输入图像描述

于 2012-05-10T00:37:38.087 回答
1

我也想知道这个。我的面板数据没有完全覆盖 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"))
于 2012-11-13T18:36:07.400 回答