Home > database >  ggplot - how to create axis ticks if I have no actual data in the plot
ggplot - how to create axis ticks if I have no actual data in the plot

Time:08-02

I have a plot where I am using just geom_hline. I use it just to illustrate the example and I don't need any actual data. This is the code:

ggplot() 
 geom_hline(yintercept = .3)

However, I want my x-axis to show ticks and text labels from 1 to 12. I just can't seem to find a way to do it - using breaks within scale_x_continuous seems to have no effect whatsoever. So then how can I make the plot that I need?

CodePudding user response:

You can use scale_x_discrete with limits and labels:

library(ggplot2)
ggplot() 
  geom_hline(yintercept = .3)  
  scale_x_discrete(limits = as.character(c(1:12)), labels = as.character(c(1:12))) 

Created on 2022-08-01 by the reprex package (v2.0.1)

  • Related