ggplot: line plot for discrete x-axis

前端 未结 1 1826
予麋鹿
予麋鹿 2021-01-12 15:53

I have the following table but after many tries have been unable to plot the data so that the x-axis tick marks line up with the year. I have found solutions to

相关标签:
1条回答
  • 2021-01-12 16:31

    Reproducible example:

    data <- data.frame(dist=c(2.914, 2.437, 2.542), year=c(2013, 2014, 2015))
    # this ensures that stuff will be ordered appropriately
    data$year <- ordered(data$year, levels=c(2013, 2014, 2015))
    ggplot(data, aes(x=factor(year), y=dist, group=1)) +
      geom_line() +
      geom_point()
    

    Specifying the year as an ordered factor will ensure that the x axis is ordered appropriately, regardless of the order in which the levels appear (whereas just using "factor(year)" in the plotting aesthetic could lead to issues).

    0 讨论(0)
提交回复
热议问题