1

Let's say I want to plot this made up population data.

dat <- data.frame(year_start_aby = c(-20, -10, 0, 10, -20, -10, 0, 10, -20, -10, 0, 10),
              year_end_aby = c(-10, 0, 10, 20, -10, 0, 10, 20, -10, 0, 10, 20),
              population = c(16000000, 19900000, 15300000, 16900000, 40000000, 55000000, 43100000, 50000000, 120000000, 125000000, 0, 0),
              planet = c("Tatooine", "Tatooine", "Tatooine", "Tatooine", "Naboo", "Naboo", "Naboo", "Naboo", "Alderaan", "Alderaan", "Alderaan", "Alderaan"))

I can easily plot population on a continuous x-axis by year_start_aby like this:

ggplot(dat, aes(year_start_aby, population, color = planet)) +
  geom_line() +
  theme_classic()

enter image description here

But if I want the x-axis to be plotted as year range factors (-20 ABY -- 0 ABY, etc.) rather than continuous year values, how can I do this?

Help me, Obi-Wan Kenobi -- you're my only hope.

mowglis_diaper
  • 479
  • 1
  • 9
  • 18
  • 2
    Try this `+scale_x_continuous(breaks=unique(dat$year_start_aby), labels=paste(unique(dat$year_start_aby),"ABY"))` – Marco Sandri Aug 26 '17 at 16:42
  • Possible duplicate of [line graph with 2 categorical variables and 1 continuous in R](https://stackoverflow.com/questions/29082083/line-graph-with-2-categorical-variables-and-1-continuous-in-r) – CrunchyTopping Jul 01 '19 at 13:41

1 Answers1

0

You have to define a group aesthetic.

ggplot(dat, aes(x = factor(year_start_aby), y = population, group = planet, color = planet)) +
geom_line()

Hope this helps.