0

I need help to align the data labels for my bar graph in ggplot to the center of each bar just above the bar. Also month in the X axis are repeating. Kindly find my ggplot code for my shiny app below,

ggplot(data = chart_m, aes(x = `Reporting Month`, y = `Total tags generating alert`, fill = HSL)) +
    geom_bar(stat = "identity", position = 'dodge') +
    geom_text(aes(label = `Total tags generating alert`), position = position_dodge(width = .9), vjust = -.1, size = 6) +
    scale_x_date(labels = date_format("%b-%y")) +
    theme_grey()

For your reference i have attached the image. I need all the data labels to be aligned to each of the bar in the center.

ggplot

expected output

enter image description here

Any inputs appreciated.

Regards,
Mohan

KenHBS
  • 6,756
  • 6
  • 37
  • 52
Mohan Raj
  • 93
  • 2
  • 10
  • [This question](http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2) could be useful. – KenHBS Jul 20 '16 at 09:18
  • 1
    I would have pointed to this question: http://stackoverflow.com/questions/12018499/how-to-put-labels-over-geom-bar-for-each-bar-in-r-with-ggplot2?rq=1 – DeveauP Jul 20 '16 at 09:30
  • @DeveauP Hi Dev, I followed the details of your link in my code. As you can see my code it is same as your link. But the only difference is that my x axis has the month values in date format which is implemented through scale_x_date(labels = date_format("%b-%y")) code. As I am using date value my data labels are not aligning correctly. – Mohan Raj Jul 20 '16 at 09:34
  • Dates and `position_dodge` take some work to dodge on the correct scale. See [here](http://stackoverflow.com/questions/8351773/r-and-ggplot-changing-x-axis-to-date-removes-position-dodge) for an example. – aosmith Jul 20 '16 at 15:21
  • @aosmith Thanks for the details.... I have written the below code for adjusting the details. ggplot(data = chart_m, aes(x = `Reporting Month`, y = `Total tags generating alert`, fill = HSL)) + geom_bar(stat = "identity", position = 'dodge') + geom_text(aes(label = `Total tags generating alert`), position = position_dodge(.9), vjust = -.02, hjust = .5, size = 6) + scale_x_date(date_breaks = "1 month", labels = date_format("%b-%y")) But, when i select multiple month values i am getting the original output screen shot which is not correct. one month correct selected correct – Mohan Raj Jul 21 '16 at 13:52
  • It will be difficult for folks to help further without a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Consider adding an example dataset to your question. – aosmith Jul 21 '16 at 14:16

0 Answers0