Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 and a Stacked Bar Chart with Negative Values

Given the following dataset:

   Division Year OperatingIncome 1  A  2012           11460 2  B  2012            7431 3  C  2012           -8121 4  D  2012           15719 5  E  2012             364 6  A  2011           12211 7  B  2011            6290 8  C  2011           -2657 9  D  2011           14657 10 E  2011            1257 11 A  2010           12895 12 B  2010            5381 13 C  2010           -2408 14 D  2010           11849 15 E  2010             517 

How do I complete a stacked bar chart in ggplot2 that includes the negative values? Here's the basic chart sequence I'm using:

ggplot(income_m, aes(x=Year, y=OperatingIncome, fill=Division)) + geom_bar() + + scale_fill_brewer(type = "seq", palette = 1) 

This returns an error:

Warning message: Stacking not well defined when ymin != 0

And instead of the expected result - negative values being displayed below the X axis - they are simply not present in the stacked bar chart. Is there a way to produce the chart with all values, both positive and negative, accounted for?

like image 806
sogrady Avatar asked Dec 05 '12 23:12

sogrady


People also ask

Can a bar graph have negative values?

Horizontal Bar Chart With Negative ValuesBar charts that display negative and positive values at the same time should be easy for the understanding and recognition of both types of values. Great way to make it clear is to divide such bar chart into a two parts by Y-Axis.

How do you graph negative data?

Negative numbers are to the left of the origin on the x-axis and below the origin on the y-axis. To graph with negative numbers, start at the origin and go to the left if the x-value is negative, and downward if the y-coordinate is negative.

What is a negative trend in a bar graph?

If the trend of data values rises from left to right, the trend is considered positive. If the trend falls from left to right, the trend is considered negative. If data points reveal no clear slope, the data is considered not correlated. A scatter chart can compare large numbers of data points without regard to time.


1 Answers

Update: As of ggplot2 2.2.0, stacking for negative values is handled automatically, without having to create separate layers for the positive and negative values.

If I understand what you're looking for, the trick is to put the two positive and negative data in separate layers, and also to use stat = "identity":

dat <- read.table(text = "   Division Year OperatingIncome 1  A  2012           11460 2  B  2012            7431 3  C  2012           -8121 4  D  2012           15719 5  E  2012             364 6  A  2011           12211 7  B  2011            6290 8  C  2011           -2657 9  D  2011           14657 10 E  2011            1257 11 A  2010           12895 12 B  2010            5381 13 C  2010           -2408 14 D  2010           11849 15 E  2010             517",header = TRUE,sep = "",row.names = 1)  dat1 <- subset(dat,OperatingIncome >= 0) dat2 <- subset(dat,OperatingIncome < 0) ggplot() +      geom_bar(data = dat1, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") +     geom_bar(data = dat2, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") +     scale_fill_brewer(type = "seq", palette = 1) 

enter image description here

like image 75
joran Avatar answered Sep 19 '22 10:09

joran