2010-04-11 60 views
3

我正在嘗試在兩個具有相同預測器的線性模型中,在迴歸器的係數ggplot2中創建一個多面圖。我構建的數據幀是這樣的:嘗試在ggplot2中創建多面圖時出錯

r.together> 
      reg   coef  se  y 
1 (Intercept) 5.068608671 0.6990873 Labels 
2  goodTRUE 0.310575129 0.5228815 Labels 
3 indiaTRUE -1.196868662 0.5192330 Labels 
4 moneyTRUE -0.586451273 0.6011257 Labels 
5  maleTRUE -0.157618168 0.5332040 Labels 
6 (Intercept) 4.225580743 0.6010509 Bonus 
7  goodTRUE 1.272760149 0.4524954 Bonus 
8 indiaTRUE -0.829588862 0.4492838 Bonus 
9 moneyTRUE -0.003571476 0.5175601 Bonus 
10 maleTRUE 0.977011737 0.4602726 Bonus 

的「Y」列是該模型的標籤,REG是迴歸量和COEF和SE是你會怎麼想。

我想繪製:

g <- qplot(reg, coef, facets=.~y, data = r.together) + coord_flip() 

但是,當我嘗試顯示的情節,我得到:

> print(g) 
Error in names(df) <- output : 
'names' attribute [2] must be the same length as the vector [1] 

什麼奇怪的是,

qplot(reg, coef, colour=y, data = r.together) + coord_flip() 

情節,你會期望。

回答

5

我不知道爲什麼,但名稱y似乎是問題所在。如果你改變它,那麼它的工作原理

r.together <- read.table(textConnection(" 
      reg   coef  se  myfactor 
(Intercept) 5.068608671 0.6990873 Labels 
    goodTRUE 0.310575129 0.5228815 Labels 
    indiaTRUE -1.196868662 0.5192330 Labels 
    moneyTRUE -0.586451273 0.6011257 Labels 
    maleTRUE -0.157618168 0.5332040 Labels 
(Intercept) 4.225580743 0.6010509 Bonus 
    goodTRUE 1.272760149 0.4524954 Bonus 
    indiaTRUE -0.829588862 0.4492838 Bonus 
    moneyTRUE -0.003571476 0.5175601 Bonus 
    maleTRUE 0.977011737 0.4602726 Bonus 
"),header=T) 

qplot(reg, coef, data = r.together, facets= .~myfactor) + coord_flip() 
+0

是的 - 這是它!謝謝! – 2010-04-11 17:55:23

+0

看起來約翰比我快。 – momobo 2010-04-11 17:59:42

+0

是的,這是一個長期存在的錯誤,我希望我能在今年夏天修好。 – hadley 2010-04-11 19:52:35

2

可能是保留字嗎?

r.together=data.frame(
    reg=c("(Intercept)", "goodTRUE", "indiaTRUE", "moneyTRUE", "maleTRUE", "(Intercept)", "goodTRUE", "indiaTRUE", "moneyTRUE", "maleTRUE"), 
    coef=c(5.068608671, 0.310575129, -1.196868662, -0.586451273, -0.157618168, 4.225580743, 1.272760149, -0.829588862, -0.003571476, 0.977011737), 
    se=c(0.6990873, 0.5228815, 0.519233, 0.6011257, 0.533204, 0.6010509, 0.4524954, 0.4492838, 0.5175601, 0.4602726), 
    yy= c("Labels", "Labels", "Labels", "Labels", "Labels", "Bonus", "Bonus", "Bonus", "Bonus", "Bonus") 
) 

YY似乎工作

g <- qplot(reg, coef, data = r.together,facets=. ~ yy) +coord_flip()