0

My data looks like this:

dat <- data.frame(
  rate = sample(1:30),
  group = sample(c("grp1", "grp2", "grp3"))
)

dat <- dat[order(dat$group),]

dat <- dat %>% group_by(group) %>% mutate(groupid = row_number())

I want to draw a line graph by groups and add text under the x-axis, I tried this:

ggplot(dat, aes(x = groupid, y = rate, group = group, colour = group)) + 
  geom_line() +
  geom_point() +
  scale_x_continuous(name = "", breaks = seq(1, 10, by = 1)) +
  annotate("text", x = 3, y = -3, size = 3.5,label = "2016")

but the text was drawn inside the graph instead of under it. enter image description here I want 2016 under the number 3 not above it. Any suggestions?

Thanks!

markus
  • 25,843
  • 5
  • 39
  • 58
mandy
  • 483
  • 9
  • 20
  • Looks like you might find an answer here... https://stackoverflow.com/questions/12409960/ggplot2-annotate-outside-of-plot/51312611#51312611 – phalteman Jan 15 '19 at 21:35
  • Thanks, I found a more intuitive answer here: https://stackoverflow.com/questions/31079210/how-can-i-add-annotations-below-the-x-axis-in-ggplot2 – mandy Jan 15 '19 at 21:38
  • The annotation is actually exactly where you said to put it: at `3` on the x-axis and `-3` on the y-axis. As the linked answers say, you can use `annotation_custom` to move it outside the plot – divibisan Jan 15 '19 at 22:30

0 Answers0