I would like to adjust the text on the barplot.
I tried to adjust hjust/vjust to display as I like it but it seems like it's not working properly.
ggplot(data) + geom_bar(aes(name, count, fill = week), stat='identity', position = 'dodge') + geom_text(aes(name,count, label=count),hjust=0.5, vjust=3, size=2, position = position_dodge(width = 1)) + coord_flip()
So I would like the numbers to situate on each bar, in the middle, at the right-edge so it's readable without overlapping like the last parts.
Text geoms are useful for labeling plots. They can be used by themselves as scatterplots or in combination with other geoms, for example, for labeling points or for annotating the height of bars. geom_text() adds only text to the plot.
Source: R/position-dodge.r , R/position-dodge2.r. position_dodge.Rd. Dodging preserves the vertical position of an geom while adjusting the horizontal position. position_dodge() requires the grouping variable to be be specified in the global or geom_* layer.
The easier solution to get hjust
/vjust
to behave intelligently is to add the group
aesthetic to geom_text
and then hjust
& position
adjust for the group
automatically.
ggplot(data) + geom_bar( aes(x = name, y = count, fill = week, group = week), stat='identity', position = 'dodge' ) + geom_text( aes(x = name, y = count, label = count, group = week), position = position_dodge(width = 1), vjust = -0.5, size = 2 ) + theme_bw()
This gives:
ggplot(data) + geom_bar( aes(x = name, y = count, fill = week, group = week), stat='identity', position = 'dodge' ) + geom_text( aes(x = name, y = count, label = count, group = week), hjust = -0.5, size = 2, position = position_dodge(width = 1), inherit.aes = TRUE ) + coord_flip() + theme_bw()
This gives:
This is not necessarily the most general way to do this, but you can have a fill
dependent hjust
(or vjust
, depending on the orientation) variable. It is not entirely clear to me how to select the value of the adjustment parameter, and currently it is based on what looks right. Perhaps someone else can suggest a more general way of picking this parameter value.
library(dplyr) library(ggplot2) # generate some data data = data_frame( week = as.factor(rep(c(1, 2), times = 5)), name = as.factor(rep(LETTERS[1:5], times = 2)), count = rpois(n = 10, lambda = 20), hjust = if_else(week == 1, 5, -5), vjust = if_else(week == 1, 3.5, -3.5) ) # Horizontal ggplot(data) + geom_bar( aes(x = name, y = count, fill = week, group = week), stat='identity', position = 'dodge' ) + geom_text( aes(x = name, y = count, label = count, vjust = vjust), hjust = -0.5, size = 2, inherit.aes = TRUE ) + coord_flip() + theme_bw()
Here is what that looks like:
ggplot(data) + geom_bar( aes(x = name, y = count, fill = week, group = week), stat='identity', position = 'dodge' ) + geom_text( aes(x = name, y = count, label = count, vjust = vjust), hjust = -0.5, size = 2, inherit.aes = TRUE ) + coord_flip() + theme_bw()
Here is what that looks like:
The position_dodge()
statement takes a width parameter. To ensure that the text is centred at the end of the bars (i.e., the dodging width for the bars and the text to be the same), give the same width parameter to the position_dodge()
statement within geom_bar
and within geom_text
.
There is also a width parameter for geom_bar
, that is the width of the bars. If you want the bars to butt up against each other within each name
, make the bar width the same as the dodging width; if you want a small gap between the bars, make the bar width a little less than the dodging width.
If you use global aesthetics, you will not need a group
aesthetic (however, using only local aesthetics, you will need a group aesthetic for geom_text
).
hjust = -0.5
will position the text labels just beyond the end of the bars; hjust = 1.5
positions them inside the end of the bars.
library(ggplot2) # Generate some data - using @tchakravarty's data - Thanks. df = data.frame( week = as.factor(rep(c(1, 2), times = 5)), name = as.factor(rep(LETTERS[1:5], times = 2)), count = rpois(n = 10, lambda = 20)) position = position_dodge(width = .75) width = .75 ggplot(df, aes(x = name, y = count, label = count, fill = week)) + geom_bar(width = width, stat='identity', position = position) + geom_text(hjust = -0.5, size = 2, position = position) + coord_flip() + theme_bw() # To separate the bars slightly: position = position_dodge(width = .75) width = .65 ggplot(df, aes(x = name, y = count, label = count, fill = week)) + geom_bar(width = width, stat='identity', position = position) + geom_text(hjust = -0.5, size = 2, position = position) + coord_flip() + theme_bw()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With