How to put labels above geom_bar for each bar in R using ggplot2

I found this How to place labels above geom_bar in R using ggplot2 , but just placed labels (numbers) on just one line.

Here, for example, two bars for each x axis, how to do the same?

My data and code are as follows:

dat <- read.table(text = "sample Types Number sample1 A 3641 sample2 A 3119 sample1 B 15815 sample2 B 12334 sample1 C 2706 sample2 C 3147", header=TRUE) library(ggplot2) bar <- ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + geom_bar(position = 'dodge') + geom_text(aes(label=Number)) 

Then we get: enter image description here

Numeric texts also seem to be located in the "evasion" pattern. I searched the geom_text manual to find some information, but can't make it work.

Suggestions?

+50
r ggplot2 bar-chart
Aug 18 '12 at 12:20
source share
2 answers

Try the following:

 ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + geom_bar(position = 'dodge', stat='identity') + geom_text(aes(label=Number), position=position_dodge(width=0.9), vjust=-0.25) 

ggplot output

+77
Aug 18 2018-12-18T00:
source share

To add rcs to the answer, if you want to use position_dodge () with geom_bar () when x is the date of POSIX.ct, you must multiply the width by 86400, e.g.

 ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + geom_bar(position = "dodge", stat = 'identity') + geom_text(aes(label=Number), position=position_dodge(width=0.9*86400), vjust=-0.25) 
+3
Jul 17 '15 at 0:57
source share



All Articles