3
votes

I'm graphing some data and want tick marks on the x-axis of my plot. My data looks like:

  publication   labels   percentage
1 foo           0       .4572
2 foo           1       .0341
3 foo           2       .09478
4 foo           3       .0135
5 bar           0       .7442
6 bar           1       .2847

in which each name has labels from 0 to 9.

My code looks like:

ggplot(aes(y = percentage, x = labels, color = publication), data = labelsdf)+
  geom_point(size = 3)+
  scale_x_discrete(breaks = c(0,1,2,3,4,5,6,7,8,9),
                   labels = c('1','2','3','4','5','6','7','8','9','10'))

But my graph looks like:

enter image description here

There weren't any tick marks without the breaks or labels specified either. Why aren't my ticks appearing?

1
It is not possible to answer this question without showing us what the data look like. A possibility is that all your actual data fall in between two specified tick marks. - Remko Duursma
@snapcrack Which version of ggplot2 are you using?? - Prradep
@Prradep 2.2.1.9000. And to Remko, I added some data; I didn't think to add it because it has straightforward discrete values but hopefully this helps. - snapcrack
publication column or vector is not present - Prradep
i renamed it to "name" accidentally and changed it back. - snapcrack

1 Answers

2
votes

After explicitly mentioning the labels as discrete using scale_x_discrete, you need to make them as discrete using factor() otherwise the values will still be numeric.

ggplot(aes(y = percentage, x = factor(labels), color = publication), data = df)+
  geom_point(size = 3)+
  scale_x_discrete(breaks = c(0,1,2,3,4,5,6,7,8,9),
                   labels = c('1','2','3','4','5','6','7','8','9','10'))

enter image description here

After using, you may wish to change the Axes labels as per your requirement.


As the labels is already discrete, it is not required to use scale_x_discrete with breaks.

ggplot(aes(y = percentage, x = labels, color = publication), data = df)+
  geom_point(size = 3)

enter image description here

If you want the x-axis labels differently (i.e., start with 1 instead of 0) from what present in the dataframe, you can get that using the small tweak as:

ggplot(aes(y = percentage, x = labels+1, color = publication), data = df)+
  geom_point(size = 3)

enter image description here