0

I have a geom_rect graph in ggplot:

data = data.frame("x"=c(1,1,1,5,5,7,7,10),
                  "y"=c(15,12,8,15,10,10,13,15),
                  "d"=c(1,2,3,4,5,6,7,8))

The code I'm using to create the plot is:

ggplot() + 
  geom_rect(data=data,mapping=aes(xmin=x,xmax=y,ymin=d-0.4,ymax=d+0.4)) 

enter image description here

Now, I want to replace the whole x axis with a specific string lets say "ABCDEFGHIJKLMNO".

with A for 1, B for 2, C for 3 etc.

Any help is appreciated.

I've tried to just put the string value next to the axis and that works. However, it's not aligned with the values.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

If you add a scale_x_continuous to your plot, you can set the breaks to 1:15, then use LETTERS[1:15] as the labels

ggplot(data) + 
  geom_rect(aes(xmin = x, xmax = y, ymin = d - 0.4, ymax = d + 0.4)) +
  scale_x_continuous(NULL, breaks = 1:15, labels = LETTERS[1:15])

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87