2013-10-20 27 views
31

也許答案只是被警告。我正在研究使用縮放和居中變量來觀察觀察值與平均值的差異。這種情節是一種常見的做法。但是當我這樣做時,我從ggplot2得到一個警告。ggplot2警告:當ymin!= 0時堆疊沒有明確定義

Warning messages: 
1: Stacking not well defined when ymin != 0 

我喜歡ggplot2和世界其他地方的快樂和沒有警告來我的方式。我嘗試通過以下方式擺脫警告,並搜索相關問題(請參閱底部的鏈接以獲取更多有希望的問題)。我的朋友ggplot2仍在警告我。

問題(S):

  1. 我怎樣才能使警告消失?
  2. 我可以忽略警告嗎?
  3. 這種做法有什麼問題嗎?

代碼嘗試:

## The data 
mtcars$scaled_mpg <- unlist(tapply(mtcars$mpg, mtcars$cyl, scale)) 
mtcars <- mtcars[order(mtcars$cyl), ] 
mtcars$ID <- unlist(tapply(mtcars$cyl, mtcars$cyl, seq_along)) 
mtcars$ID <- factor(sprintf("%02d", mtcars$ID)) 

## ================ Attempt 1 ================ 
ggplot(mtcars, aes(x = ID, y = scaled_mpg, fill = factor(cyl))) + 
    geom_bar(stat="identity") + facet_grid(cyl~.) 

## ================ Attempt 2 ================  
ggplot(mtcars, aes(x = ID, fill = factor(cyl))) + 
    geom_bar(aes(weight = scaled_mpg)) + facet_grid(cyl~.) 

## ================ Attempt 3 ================ 
dat1 <- subset(mtcars, scaled_mpg >= 0) 
dat2 <- subset(mtcars, scaled_mpg < 0) 

ggplot() + 
    geom_bar(data = dat1, aes(x = ID, y = scaled_mpg, 
     fill = factor(cyl)),stat = "identity") + 
    geom_bar(data = dat2, aes(x = ID, y = scaled_mpg, 
     fill= factor(cyl)),stat = "identity") + 
    facet_grid(cyl~.) 

情節:

enter image description here

類似的帖子:

+3

是的。在這種情況下,警告將被忽略。它只是在那裏讓人們注意到他們可能正在通過對barcharts使用非零參考來實施撒謊統計策略。哎呀,你添加facet後甚至沒有堆疊。 –

回答

29

1)無論是addingposition = "identity"geom_bar或者,當然,通過使用

suppressWarnings(print(ggplot(...))) 

2-3)考慮到技術方面 - 是的,你可以忽略它。此警告的reason與解釋條形具有負值高度而非僅負值有關。