-4

I would like to have the name of each bar under each bar (in my case the names are "Round" and they happen to be 1, 2, ... 12)

Here is my current code:

ggplot(data=draft1, aes(x = Round, y = mean.age)) + 
geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
ylab("Average age of retirement") +   ylim(c(0,40)) + 
ggtitle("Average age of retirement by rounds of all players") +
geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5)

Here is the current output: enter image description here

Developer
  • 917
  • 2
  • 9
  • 25
TDo
  • 686
  • 4
  • 9
  • 22

2 Answers2

1

set your Round to be a factor

ggplot(data=draft1, aes(x = factor(Round), y = mean.age)) + 

Or use scale_x_continuous()

ggplot(data=draft1, aes(x = Round, y = mean.age)) + 
  ... +
  scale_x_continuous(breaks=(seq(1:12)))
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
1

Just add a discrete scale to x:

library(ggplot2)

draft1 = data.frame(Round = seq(1, 12), mean.age = sample(29:32))

ggplot(data=draft1, aes(x = Round, y = mean.age)) + 
  geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
  ylab("Average age of retirement") +   ylim(c(0,40)) + 
  ggtitle("Average age of retirement by rounds of all players") +
  geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5) +
  scale_x_discrete()

enter image description here

Liesel
  • 2,929
  • 2
  • 12
  • 18