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

3
  • 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 for more info. Commented Jul 20, 2020 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? Commented Jul 20, 2020 at 20:07
  • 1
    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 , .... Commented Jul 20, 2020 at 20:19

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the step by step procedure
2
ggplot(...) + geom_line(...) + scale_x_continuous(n.breaks = 30) 

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.