2012-11-03 4 views
0

R에 플롯 기능에 문제가 있습니다. 지금까지 내가 무엇을 얻었습니다.간단한 플롯 기능 있음 R

countries <- c("CHINA ", "UNITED STATES", "UNITED KINGDOM", "GERMANY") 
KeyItems <- c("Pretax Income", "Total Liabilities", "Total Assets") 
datA <- mean_values[mean_values$Country %in% countries & mean_values$KeyItem %in% KeyItems, ] 
datA$KeyItem <- factor(datA$KeyItem, levels = KeyItems, order = TRUE) 
p <- xyplot(mn_value ~ Year | KeyItem, datA, groups = datA$Country[, drop = TRUE], 
      auto.key = list(space = "right"), par.settings = simpleTheme(pch = 1:5), 
      type = c("p", "l"), as.table = TRUE) 
print(p) 

내 dataframe은 다음과 같습니다 :이 오류를 반환

KeyItem   Year  Country          mn_value 
172 Pretax Income 1980 SWITZERLAND         2.091623e+08 
173 Pretax Income 1980 IRELAND          3.597619e+07 
174 Pretax Income 1980 GERMANY          2.301015e+07 
175 Pretax Income 1980 SWEDEN          4.980680e+07 

:

Error in dat$Year == Year : 'Year' is missing 

내가 거의 R.의 경험이 난 그냥에 대한 수정을 찾을 수 없습니다가 내 문제. 미리 감사드립니다.

+3

예제를 작성하는 데 더 많은 시간을 할애해야한다고 생각합니다. 예제 코드가 전혀 실행되지 않기 때문입니다 (음모를 내기 훨씬 전에). 나는 정확히 'datA'가 무엇인지에 특히주의를 기울일 것이다. – joran

+2

재현 가능한 예하시기 바랍니다! 나는 당신의 코드를 시도 할 때 ** 에러가 없다 ** - 아마도 코드에서 라인을 생략하고있을 것이다 ('xyplot' 호출 외에도'Year'에 대한 언급이 없으며 에러 메시지는 실제로 일치하지 않는다. 귀하의 코드와 함께). 어떤 코드 줄에서 오류가 발생하는지 파악할 수 있습니까? –

+0

'dat' 또는'datA'입니까? –

답변

1

다른 사람이 언급했듯이 코드는 분명히 불완전하기 때문에 정확하지 않은 방식으로 범주 내에 mean 값을 구성하는 것으로 나타납니다. 그래서 여기에 일부 (전부는 아니지만) 방법으로 문제를 닮았다 있다는 xyplot까지 이어지는 일 예이다 : 그것은 라벨 몇 가지 추가 작업이 필요거야하지만 당신이 때까지 아마 연기 할 수

Values <- rpois(100*4*3, 200) 
datA=data.frame(Values=Values, countries=countries, KeyItems=KeyItems) 
datAaggr <- with(datA, aggregate(Values , list(KeyItems, countries) , FUN=mean)) 
# At this point you could rename the Group variables, 
# or you could have done that in the aggregate call with: 
datAaggr <- with(datA, aggregate(Values , list(KeyItems=KeyItems, countries=countries) , FUN=mean)) 
# This then succeeds using the aggregated dataframe with the re-named mean values as input: 

p <- xyplot(x ~ countries | KeyItems, data=datAaggr, 
      auto.key = list(space = "right"), par.settings = simpleTheme(pch = 1:5), 
      type = c("p", "l"), as.table = TRUE) 
print(p) 

플로팅 작업에 앞서 학습 작업 인 data.frames를 사용하여 데이터 변환을 수행하는 방법을 배웠습니다. 래티스 플로팅 시스템은 올바른 data.frame 입력이 있어야합니다.