2015-12-24 35 views
0

在讀一本書時,我發現這個代碼。我嘗試了一下,並對即時獲取的圖有點困惑。Y軸錯誤索引

這是數據示例。

consumption[sample(1:nrow(consumption), 5, replace=F),] 
        Food Units Year Amount 
8 Fruits and Vegetables Pounds 1980 603.57948 
31 Caloric sweeteners Pounds 1995 144.08113 
16 Fruits and Vegetables Pounds 1985 630.24491 
28     Eggs Number 1995 232.28203 
19 Fish and Shellfist Pounds 1990 14.94411 

enter image description here

並即時得到此圖。 Y指數是從1到20的數字,不是正確的「金額」。

enter image description here

我能做些什麼,所以Y軸的數量指數顯示正確?

+1

我會需要我的眼鏡:)你可以在你的文章中重現代碼,而不是使用圖像。你還可以使用dput()在你的文章中包含你的數據集。 – MLavoie

回答

0

您顯示的數字就像書中提供的代碼R in a Nutshell一樣。實際上,這本書提供了同一劇情的兩個不同版本的代碼。我建議兩種都嘗試。

library(nutshell) 
data(consumption) 

library(lattice) 
dotplot(Amount ~ Year | Food, consumption) 
dotplot(Amount ~ Year | Food, consumption, 
    aspect="xy", scales=list(relation="sliced", cex=.4)) 
+0

是的,沒有。唯一的問題是,Y軸顯示的數字從1到49而不是正確的數量,如645等。 –

+1

'dotplot'用於顯示「類別」。如果要顯示散點圖中的值,請嘗試'xyplot',例如'xyplot(Amount〜Year | Food,consumption)'。 –

+0

是@Jean V. Adams多數民衆贊成正確!謝謝 –