Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 warning: Stacking not well defined when ymin != 0

Tags:

r

ggplot2

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:

  • set length of `geom_hline` in `geom_bar` plot
  • ggplot2 - stacking not well defined when ymin !=0
  • ggplot2 and a Stacked Bar Chart with Negative Values
like image 775
Tyler Rinker Avatar asked Oct 20 '13 19:10

Tyler Rinker


1 Answers

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.

like image 199
Julius Vainora Avatar answered Nov 02 '22 10:11

Julius Vainora