Graphics Reference
In-Depth Information
and not change the vertical justification. One drawback to changing the text's yposition is that if
you want to place the text fully above or below the bar top, the value to add will depend on the y
range of the data; in contrast, changing vjust to a different value will always move the text the
same distance relative to the height of the bar:
# Adjust y limits to be a little higher
ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
geom_bar(stat = "identity" ) +
geom_text(aes(label = Weight), vjust = -0.2 ) +
ylim( 0 , max(cabbage_exp$Weight) * 1.05 )
# Map y positions slightly above bar top - y range of plot will auto-adjust
ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
geom_bar(stat = "identity" ) +
geom_text(aes(y = Weight + 0.1 , label = Weight))
For grouped bar graphs, you also need to specify position=position_dodge() and give it a
value for the dodging width. The default dodge width is 0.9. Because the bars are narrower, you
might need to use size to specify a smaller font to make the labels fit. The default value of size
is 5, so we'll make it smaller by using 3 ( Figure 3-23 ):
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity" , position = "dodge" ) +
geom_text(aes(label = Weight), vjust = 1.5 , colour = "white" ,
position = position_dodge( .9 ), size = 3 )
Search WWH ::




Custom Search