2013-08-27 36 views
0

我正在做一個功能數據分析項目,我正在試圖繪製高度的意大利麪圖。我使用格子庫中的xyplot。爲什麼y軸被包裹在xyplot中?爲什麼縱軸數據中的y軸包裹在xyplot中?

這裏我只繪製了一個人的數據。如果繪製整個數據集,它看起來就像一大塊粗線。

我的R中的代碼是:

xyplot(height ~ age|sex, p_data, type="l", group=id) 

,導致:

enter image description here

回答

2

沒有看到p_data這很難說,但基於軸標籤我猜想,height是被視爲一個因素變量。

運行is.factor(p_data$height),如果答案是TRUE然後嘗試

p_data$height <- as.numeric(levels(p_data$height))[p_data$height]

和重複你的陰謀。如果這不起作用,那至少可以給我們一些關於p_data數據幀的信息。

+0

事實上,這是一個問題,這是一個因素。謝謝喬! – Javas

1

@Joe已經把你放在正確的道路上。問題幾乎可以肯定的是,height變量被當作一個因子(分類變量)而不是一個連續的數字變量:

E.g. - 我可以通過複製一個類似的問題:

p_data <- data.frame(height=c(96,72,100,45),age=1:4,sex=c("m","f","f","m"),id=1) 
p_data$height <- factor(p_data$height,levels=p_data$height) 

# it's all out of order cap'n! 
p_data$height 
#[1] 96 72 100 45 
#Levels: 96 72 100 45 

# same plot call as you are using  
xyplot(height ~ age|sex, p_data, type="l", group=id) 

enter image description here

如果你修復它,像這樣:

p_data$height <- as.numeric(as.character(p_data$height)) 

....然後在同一個呼叫給出相應的結果:

xyplot(height ~ age|sex, p_data, type="l", group=id) 

enter image description here

+0

謝謝!身高是一個因素。 – Javas