0

我在使用 ggplot2 进行“串行”绘图时遇到问题。这是我的df:

    structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L), .Label = c("P1", "P21", "P24", "P25"
), class = "factor"), Date = structure(c(21L, 22L, 24L, 25L, 
28L, 29L, 30L, 31L, 32L, 33L, 34L, 1L, 2L, 3L, 4L, 6L, 7L, 8L, 
9L, 10L, 12L, 22L, 23L, 26L, 27L, 29L, 30L, 31L, 32L, 1L, 2L, 
3L, 4L, 5L, 7L, 9L, 8L, 11L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L), .Label = c("1996-05-30", "1996-12-06", "1997-03-18", "1997-06-27", 
"1997-09-29", "1997-09-30", "1997-11-24", "1998-03-13", "1998-05-07", 
"1998-07-09", "1998-07-14", "1998-10-26", "1998-10-30", "1999-03-15", 
"1999-06-16", "1999-09-06", "1999-11-10", "2000-03-15", "2000-06-21", 
"2000-11-28", "2007-09-04", "2007-10-16", "2008-05-21", "2008-05-22", 
"2008-08-06", "2008-08-12", "2008-10-16", "2008-10-17", "2009-07-01", 
"2009-10-14", "2010-07-02", "2010-09-29", "2011-06-09", "2011-08-23"
), class = "factor"), T = c(11.1, 11.1, 10.9, 10.9, 10.6, 11, 
10.5, 11.2, 10.9, 10.7, 11.1, 10.9, 10.2, 10.2, 10.9, 10.2, 9.9, 
10, 10.3, 10.5, 10.1, 11.1, 11.1, 11.1, 10.9, 11.1, 10.7, 11.3, 
11, 11.4, 10.2, 10.2, 10.7, 10.3, 9.9, 10.2, 10.1, 10.2, 10.2, 
10.2, 10.6, 10.7, 10.2, 10.3, 11, 10.6), ph = c(6.76, 6.72, 6.9, 
6.91, 6.96, 6.98, 6.94, 7.02, 7, 6.92, 6.94, 6.5, 6.4, 6.7, 6.52, 
6.6, 6.6, 6.5, 6.55, 6.51, 6.59, 6.72, 6.76, 6.82, 6.8, 6.76, 
6.76, 6.88, 6.82, 6.7, 6.7, 6.9, 6.71, 6.9, 6.8, 6.7, 6.69, 6.79, 
6.69, 6.68, 6.5, 6.67, 6.65, 6.73, 6.78, 6.68), EC = c(1499L, 
2120L, 881L, 902L, 870L, 541L, 891L, 876L, 860L, 868L, 877L, 
3630L, 3400L, 2470L, 2330L, 1810L, 2190L, 2810L, 2200L, 2440L, 
1111L, 2120L, 1654L, 1746L, 1781L, 761L, 1627L, 1733L, 1633L, 
2440L, 3130L, 3180L, 2530L, 2710L, 2450L, 2630L, 3610L, 2190L, 
973L, 3650L, 3060L, 3280L, 2930L, 879L, 3040L, 3030L), Month = structure(c(9L, 
8L, 6L, 1L, 8L, 3L, 8L, 3L, 9L, 4L, 1L, 6L, 2L, 5L, 4L, 9L, 7L, 
5L, 6L, 3L, 8L, 8L, 6L, 1L, 8L, 3L, 8L, 3L, 9L, 6L, 2L, 5L, 4L, 
9L, 7L, 6L, 5L, 3L, 8L, 5L, 4L, 9L, 7L, 5L, 4L, 7L), .Label = c("August", 
"December", "July", "June", "March", "May", "November", "October", 
"September"), class = "factor"), Year = c(2007L, 2007L, 2008L, 
2008L, 2008L, 2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 1996L, 
1996L, 1997L, 1997L, 1997L, 1997L, 1998L, 1998L, 1998L, 1998L, 
2007L, 2008L, 2008L, 2008L, 2009L, 2009L, 2010L, 2010L, 1996L, 
1996L, 1997L, 1997L, 1997L, 1997L, 1998L, 1998L, 1998L, 1998L, 
1999L, 1999L, 1999L, 1999L, 2000L, 2000L, 2000L)), .Names = c("ID", 
"Date", "T", "ph", "EC", "Month", "Year"), class = "data.frame", row.names = c(NA, 
-46L))

我想要做的是一个函数,它为每个 ID、EC 值与月份进行绘图并将它们按年分组。首先,我在有序因子中更改了 Month 列:

df$Month<-factor(df$Month, levels=month.name, ordered=T)

然后我尝试用这段代码做到这一点:

by (df,df$ID,function(i){
  ggplot(df) +
           geom_point(aes(i$Month, i$EC, group=i$Year))
         })

但我收到一个错误。奇怪的是,相同的代码适用于 lattice:

by (df,df$ID,function(i){
  xyplot(i$EC~i$Month, data=df, group=i$Year, main=list(unique(i$ID)),
         xlab="Months", ylab="EC",type=c('p','l','g'),
         auto.key=list(columns=3,lines=TRUE))
})

我真的不知道我在哪里缺少 ggplot。有什么建议么?

4

1 回答 1

1

您应该进行i内部ggplot()调用,因为这是现在子集数据框的名称。而且你不需要使用i$inside aes()

by(df,df$ID,function(i) {
  ggplot(i) +
          geom_point(aes(Month, EC, group=Year))
})
于 2013-08-27T15:50:06.550 回答