-1

I have a dataframe like shown below

    Quarter X2013        X2014    Total.Result
1   Qtr 1   77282.13    66421.10    143703.2
2   Qtr 2   69174.64    76480.13    145654.8
3   Qtr 3   65238.47    79081.74    144320.2
4   Qtr 4   65429.73    109738.82   175168.5

And I generated a barplot by melting the data-frame as shown below. But the problem lies in annotating the bar values as shown in the flowing pic.

dfm <- melt(qtr2, id.vars = c('Quarter', 'Total.Result'))

#ggplot statement
{
  ggplot(dfm, aes(x = factor(Quarter), y = value, fill = variable) ) +
  geom_bar(stat="identity", position = 'dodge') + theme_bw() +
    geom_text(aes(label=value,angle = 90), vjust=0)+
    ylab("Sales ($)") +
    xlab("Quarter")+
    ggtitle(label1) + 
    theme(plot.title = element_text(size=20,lineheight=.8, face="bold"))+
    scale_fill_discrete(name="Years",
                        breaks=c("X2013", "X2014"),
                        labels=c("2013", "2014")) +
    theme(legend.background = element_rect(fill="gray90", 
                                           size=.5, linetype="dotted"))+
    theme(axis.title.x = element_text(face="bold", colour="#990000", size=20),
          axis.text.x  = element_text(angle=90, vjust=0.5, size=16))+
    theme(axis.title.y = element_text(face="bold", colour="#990000", size=20),
          axis.text.y  = element_text(angle=90, vjust=0.5, size=16))+
    scale_y_continuous(labels=dollar)
}

The output plot enter image description here How do I set proper position and font styles to the geom_text ?


The .csv file for the sample data to import into R

"Quarter","2013","2014","Total Result"
"Qtr 1", 77282.13, 66421.10, 143703.23
"Qtr 2", 69174.64, 76480.13, 145654.77
"Qtr 3", 65238.47, 79081.74, 144320.21
"Qtr 4", 65429.73, 109738.82, 175168.55
sunitprasad1
  • 768
  • 2
  • 12
  • 28
  • 1
    You need to create a [minimal reproducible example](http://stackoverflow.com/a/5963610/1412059). "Minimal" means that you can remove most of the ggplot code, because only the calls to `geom_bar` and `geom_text` are necessary to reproduce the actual problem. "Reproducible" means that we can just copy your code into a fresh R session and see the same output as you. Anyway, you should use `position_dodge` within `geom_text`. – Roland Apr 11 '15 at 09:35
  • sure, will take care of that. – sunitprasad1 Apr 21 '15 at 10:30

1 Answers1

1

This should work, however I am not using angle.

ggplot(dfm,aes(factor(Quarter),value,fill=variable)) +  
   geom_bar(stat="identity",position=position_dodge()) + 
   geom_text(aes(y=value,ymax=value,label=value),
        position=position_dodge(width=0.9),vjust=0.9) +
   ylab("Sales ($)") +
   xlab("Quarter")+
   ggtitle("label1") + 
   theme_bw() +
   theme(plot.title = element_text(size=20,lineheight=.8, face="bold"),
        legend.background = element_rect(fill="gray90", size=0.5, linetype="dotted"),
        axis.title.x = element_text(face="bold", colour="#990000", size=20),
        axis.text.x  = element_text(angle=90, vjust=0.5, size=16),
        axis.title.y = element_text(face="bold", colour="#990000", size=20),
        axis.text.y  = element_text(angle=90, vjust=0.5, size=16))
pogibas
  • 27,303
  • 19
  • 84
  • 117