31

Perhaps the answer is to just be warned. I am attmepting to use a scaled and centered variable to look at how observations differ from the mean value. This plot is a common practice. But when I do this I get a warning from ggplot2.

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

I like to have ggplot2 and the rest of the world happy and no warnings coming my way. I tried to get rid of the warning in the following ways and searched SO (see links at bottom for some more promising questions) for related questions. Still my friend ggplot2 is warning me.

QUESTION(S):

  1. How can I make the warning go away?
  2. Can I ignore the warning?
  3. Is there something wrong with this practice?

Code attempts:

## 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~.)

The plot:

enter image description here

Similar posts:

1
  • 3
    Yes. The warning is to be ignored in this case. It's just in there to put people on notice that they may be committing the lying-with-statistics strategy by using a non-zero reference for barcharts. Heck, you aren't even stacking after adding the facets.
    – IRTFM
    Oct 20, 2013 at 20:49

1 Answer 1

29

1) Either by adding position = "identity" to geom_bar or, of course, by using

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

2-3) Considering the technical side - yes, you can ignore it. The reason for this warning is related to interpreting that bars have negative height instead of just negative values.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.