2013-10-20 64 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与解释条形具有负值高度而非仅负值有关。