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  )

ここに画像の説明を入力

このアプローチは非常に複雑に思えます。ベース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 に答える