2013-08-27 3 views
0

ggplot2로 "연속"플롯에 문제가 있습니다.ggplot2를 사용한 배치 플롯

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 값 년에서 그들을 그룹화입니다 : 이것은 내 DF이다. 우선은 주문 요인에 달 열을 변경 :

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

하지만 오류가 발생합니다. 어떤 이상한 것은 동일한 코드는 격자 작동한다는 것입니다 :

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와 누락 곳 난 정말 모르겠어요. 어떤 제안?

답변

1

ggplot() 내부에 넣어야합니다. 이것은 하위 집합 된 데이터 프레임의 이름이기 때문에 호출하십시오. 그리고 안에 i$을 사용할 필요가 없습니다.

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