0

I am using ggplot (Line graph) and trying to plot my data by week, however when I am plotting the data R automatically shows the weeks by 10, 15, .... I want o show all the weeks number on my X Axis, e.g. 10,11,12,...

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32
Pegah
  • 53
  • 1
  • 1
  • 4
  • Hi Pegah, welcome to Stack Overflow. It will be much easier to help if you provide at least a sample of your data with `dput(data)` or if your data is very large `dput(data[1:20,])`. You should replace `data` with the name of your actual data. You can [edit] your question and paste the output. Please surround the output with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell Jul 20 '20 at 20:07
  • Hi OP - can you share some of your dataset or at least the structure of the column that corresponds to your x axis? Is it just numeric, or is it a Date/POSIXct class? – chemdork123 Jul 20 '20 at 20:07
  • Hi, I added the picture in my question, my X Axis data is already Week number, (10, 11, 12, ....) , R Automatically hide the week numbers and just show 10, 15 , .... – Pegah Jul 20 '20 at 20:19

2 Answers2

3

It seems your "weeks" axis is numeric (just the number) rather than a date. To change where the tick marks are indicated for your axis, you can use the breaks= argument of scale_*_continuous() for the numeric scale. Here's an example where you can see how to do this:

df <- data.frame(x=1:20, y=rnorm(20))
p <- ggplot(df, aes(x,y)) + geom_point()
p

enter image description here

By default, the x axis is separated into major breaks of 5. If you wanted breaks every 1, you supply a vector to the breaks= argument:

p + scale_x_continuous(breaks=seq(0,20,by=1))

enter image description here

You can even do odd things, like specify breaks individually if you want:

p + scale_x_continuous(breaks=c(0,5,10,11,12,18,20))

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32
1
ggplot(...) + geom_line(...) + scale_x_continuous(n.breaks = 30)

You can modify the n.breaks parameter to your liking.