2012-11-03 62 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) 

我的數據幀是這樣的:

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 

我幾乎沒有在任何河的經驗,我只是不能找到一個解決我的問題。先謝謝你。

+3

我認爲你需要花更多的時間來創建一個例子,因爲你的例子代碼根本不運行(在你開始繪製命令之前)。我會特別注意'datA'的含義。 – joran

+2

請重複舉例!當我嘗試你的代碼時,我得到了**沒有錯誤** - 也許你在代碼中省略了一行(除了在你的'xy​​plot'調用中沒有提及'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進行數據轉換,這是繪圖任務之前的學習任務。萊迪思繪圖系統嚴格依賴於擁有正確的數據幀輸入。