user1234440
user1234440

Reputation: 23567

Stacked barplot

I took the following sample code from another similar question and tried to replicate it.

x <- structure(list(variable = c("a", "b", "c"), f = c(0.98, 0.66, 0.34), m = c(0.75760989010989, 0.24890977443609, 0.175125)), .Names = c("variable","f", "m"), row.names = c(NA, 3L), class = "data.frame")

> x
  variable    f         m
1        a 0.98 0.7576099
2        b 0.66 0.2489098
3        c 0.34 0.1751250

and when the example ran the following code:

ggplot(x, aes(variable, f,label=variable)) + 
  geom_bar() + geom_bar(aes(variable, m), fill="purple")

the following stacked bar char shows...

enter image description here


My turn! The following is a data frame of data

> data
               asset.std           asset.dstd symbols
IEF  0.00470368279997122  0.00477691934631662     IEF
SPY   0.0119358320227236   0.0130162006829043     SPY
GSG   0.0137798134700255   0.0147096635302501     GSG
VNQ    0.016058588692544   0.0169327904112519     VNQ
TLT   0.0108803682930942   0.0109165197621356     TLT
SHY 0.000635574928974698 0.000676146828833939     SHY 

and i run the following code

ggplot(data, aes(symbols, asset.std, label=symbols))+
  geom_bar() +   geom_bar(aes(symbols, asset.dstd),fill="blue")

and i get this instead....huh? enter image description here

Am i missing something in my ggplot2 code? Anything would help thxs

Upvotes: 1

Views: 921

Answers (2)

Brian Diggs
Brian Diggs

Reputation: 58825

A few points. First, I think that your data is not what you think it is; asset.std and/or asset.dstd look like they are factors rather than numbers. If you look at str(data), you will probably see that those variables are factors. If I read you data in and plot it, I get a different result.

data <- read.table(text=
"               asset.std           asset.dstd symbols
IEF  0.00470368279997122  0.00477691934631662     IEF
SPY   0.0119358320227236   0.0130162006829043     SPY
GSG   0.0137798134700255   0.0147096635302501     GSG
VNQ    0.016058588692544   0.0169327904112519     VNQ
TLT   0.0108803682930942   0.0109165197621356     TLT
SHY 0.000635574928974698 0.000676146828833939     SHY", header=TRUE)

ggplot(data, aes(symbols, asset.std, label=symbols))+
  geom_bar() +   geom_bar(aes(symbols, asset.dstd),fill="blue")

enter image description here

Using the output of dput (as in the example you cite) eliminates that problem.

data <-
structure(list(asset.std = c(0.00470368279997122, 0.0119358320227236, 
0.0137798134700255, 0.016058588692544, 0.0108803682930942, 0.000635574928974698
), asset.dstd = c(0.00477691934631662, 0.0130162006829043, 0.0147096635302501, 
0.0169327904112519, 0.0109165197621356, 0.000676146828833939), 
    symbols = structure(c(2L, 4L, 1L, 6L, 5L, 3L), .Label = c("GSG", 
    "IEF", "SHY", "SPY", "TLT", "VNQ"), class = "factor")), .Names = c("asset.std", 
"asset.dstd", "symbols"), class = "data.frame", row.names = c("IEF", 
"SPY", "GSG", "VNQ", "TLT", "SHY"))

Second, as @mrdwab said, this isn't a stacked bar plot. This is just two sets of bars drawn on top of each other. The set drawn first is smaller and so is completely hidden by the second set. Making the bars partly transparent makes this apparent.

ggplot(data, aes(symbols, asset.std, label=symbols))+
  geom_bar(alpha=0.25) + 
  geom_bar(aes(symbols, asset.dstd),fill="blue", alpha=0.25)

If you really want stacked bars, melt the data and then plot it.

library("reshape2")
ggplot(melt(data, id.vars="symbols"),
       aes(symbols, value, fill=variable)) +
  geom_bar()

enter image description here

Upvotes: 4

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

Looking at your object data:

DF[1] - DF[2]
#         asset.std
# IEF -7.323655e-05
# SPY -1.080369e-03
# GSG -9.298501e-04
# VNQ -8.742017e-04
# TLT -3.615147e-05
# SHY -4.057190e-05

In all cases, asset.std is less than asset.dstd; thus, if you plotted asset.std first, when you plot the second column on top of that, you would just entirely cover up the first plot!

To replicate the example you've provided, plot asset.dstd first:

ggplot(DF, aes(symbols, asset.dstd, label=symbols)) + 
  geom_bar(fill="red") + 
  geom_bar(aes(symbols, asset.std), fill="blue", position="stack")

Note, however, that this is not a stacked bar chart in the sense that the term is commonly used.

Upvotes: 3

Related Questions