Creating a bar chart within another bar chart

5

I'm looking for a very efficient and useful way to use nested bar charts. A way in which you can demonstrate divisions and subdivisions in the same chart. How to generate such a graph in R?

    
asked by anonymous 22.09.2014 / 20:13

1 answer

6

Toproducetheabovegraph,simplyfollowtheexamplebelow:

install.packages('ggplot2')library(ggplot2)install.packages("reshape")
library(reshape)
install.packages("scales")
library(scales)

#.................
# Exemplo de dados

df <- read.csv(textConnection("Segment,Alpha,Beta,Gamma,Delta
                                 A,1416649,590270,236108,118054
                                 B,708325,531243,354162,177081
                                 C,354162,354162,236108,236108
                                 D,147568,147568,147568,147568"))
closeAllConnections()

#.........................................................
# Colocando tudo em uma mesma escala e rotulando os eixos

dfa <- melt(df, id = c("Segment"), variable_name = "Company")

#...................
# Tratando do eixo Y

ylim <- max(cast(dfa, Company ~ ., sum)[, 2],
            cast(dfa, Segment ~ ., sum)[, 2])

#.........
# Plotando

p <- ggplot(dfa) + scale_y_continuous(limits = c(0,ylim), breaks = NULL)
p1 <- p + geom_bar(aes(Company, value), stat = "identity",breaks = NULL)
(p2 <- p1 + geom_bar(aes(Company, value, fill = Segment),
                     stat = "identity",breaks = NULL, position = "dodge"))
    
22.09.2014 / 20:16