32

I would like to do a bar plot outlined in black with percentages inside the bars. Is this possible from qplot? I get the percentages to appear but they don't align with the particular bars.

packages: ggplot2, reshape

created in Illustrator

x <- data.frame(filename = c("file1", "file2", "file3", "file4"),
                    low = c(-.05,.06,.07,-.14),
                    hi = c(.87,.98,.56,.79))
x$tot <- x$hi + x$low

x <- melt(x, id = 'filename')

bar <- qplot(x = factor(filename), 
             y = value*100,
             fill = factor(variable),
             data = x,
             geom = 'bar',
             position = 'dodge') + coord_flip()
bar <- bar + scale_fill_manual(name = '',
                               labels = c('low',
                                          'Hi',
                                          "Tot"),
                               values = c('#40E0D0',
                                          '#FF6347',
                                          "#C7C7C7")) 
bar <- bar + geom_text(aes(label = value*100))+geom_bar(colour = 'black')
bar <- bar + opts(panel.background = theme_rect(colour = NA))
bar <- bar + opts(legend.justification = 'bottom')
print(bar)
Andrie
  • 176,377
  • 47
  • 447
  • 496
Michael Street
  • 645
  • 1
  • 7
  • 19
  • 3
    Welcome to SO. Since you are making use of non base R functions, please add a reference to the packages needed to reproduce your code, i.e. `library(...)` – Andrie Jul 25 '12 at 15:32

2 Answers2

50

Here you go:

library(scales)
ggplot(x, aes(x = filename, fill = variable)) +
  geom_bar(stat="identity", ymin=0, aes(y=value, ymax=value), position="dodge") +
  geom_text(aes(x=filename, y=value, ymax=value, label=value, 
                hjust=ifelse(sign(value)>0, 1, 0)), 
            position = position_dodge(width=1)) +
  scale_y_continuous(labels = percent_format()) +
  coord_flip()

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
Andrie
  • 176,377
  • 47
  • 447
  • 496
6

This would be a good opportunity for you to start moving away from using qplot, in favor of ggplot. This will be much easier in the long run, trust me.

Here's a start:

library(scales)
ggplot(data = x,aes(x = factor(filename),y = value)) + 
    geom_bar(aes(fill = factor(variable)),colour = "black",position = 'dodge') + 
    coord_flip() + 
    scale_fill_manual(name = '',
                      labels = c('low',
                                 'Hi',
                                 "Tot"),
                      values = c('#40E0D0',
                                 '#FF6347',
                                 "#C7C7C7")) + 
    scale_y_continuous(labels = percent_format())

For philosophical reasons, I will leave the annotation piece to you...

joran
  • 169,992
  • 32
  • 429
  • 468
  • Thanks. There still isn't text/percentages on the plot. Do I pass 'value' to the percent_format()? – Michael Street Jul 25 '12 at 15:54
  • 1
    @RwardBound I know. I deliberately omitted the text labels because I am philosophically opposed to labeling bar charts in that manner. (But if you _were_ to make the labels, you wouldn't use anything in `scale_y_continuous`.) – joran Jul 25 '12 at 15:58
  • 1
    I see. What do you personally prefer? And how would I add the text please? very grateful for your help. – Michael Street Jul 25 '12 at 16:02
  • Thanks for the help with the outline, but I gave the check to Andrie. – Michael Street Jul 25 '12 at 16:10
  • @RwardBound No problem! I suspected someone else would show you. – joran Jul 25 '12 at 16:24