12

I have data with continuous x and y values. Over a specific x interval, I want to make the ticks increments to be smaller, e.g. from 50 to 60, the distance between the breaks should be 1 (50, 51, 52, 53 ... 59, 60). For the rest of the axis, it is fine to have the ticks incremented by 10. My desired x-axis would have breaks at:

10,20,30,40,50,51,52,53,54,55,56,57,58,58,60,70,80,90,..190,200

What I have tried:

x <- seq(1:200)
y <- seq(51, 250, by = 1)
df <- data.frame(x = x, y = y)

ggplot(data = df, aes(x, y)) +
  geom_line(size=1.6)+ 
  scale_x_continuous(breaks = c(10, 20, 30, 40, seq(50, 60, by = 2), seq(70, 200, 10)),
                     minor_breaks = seq(50, 60, by = 2)) +
theme(axis.text.x = element_text(size = 16),
      axis.text.y = element_text(size = 16),
      axis.title.x = element_text(size = 16),
      axis.title.y = element_text(size = 16),
      axis.ticks.x = element_line(size = 1),
      axis.ticks.length = unit(0.8, "cm")) + 
xlab("Time") + ylab("value")+

graph

As you see, the labels are overlapping. How can I achieve this in a clearer way?

Henrik
  • 65,555
  • 14
  • 143
  • 159
SimpleNEasy
  • 879
  • 3
  • 11
  • 32

1 Answers1

27

It seems very tight to squeeze in more labels than every 10. So you may try to drop the labels at tickmark 52 to 58, by labelling these four positions with ""

ggplot(data = df, aes(x = x, y = y)) +
  geom_line() + 
  scale_x_continuous(breaks = c(seq(from = 10, to = 200, by = 10),
                                seq(from = 52, to = 58, by = 2)),
                     labels = c(seq(from = 10, to = 200, by = 10), rep("", 4)))

enter image description here

Alternatively, you can zoom in on the relevant x-range using coord_cartesian. The underlying data is unchanged, and we just magnify a small section of the original data. The zoomed-in plot can then be added to the original plot as a subplot. There are many ways to arrange subplots. Here is one example:

# The original plot on full range of x
g1 <- ggplot(data = df, aes(x = x, y = y)) +
  geom_line() 

# zoom in to the relevant section of x 
g2 <- ggplot(data = df, aes(x = x, y = y)) +
  geom_line() +
  coord_cartesian(xlim = c(49, 61)) +
  scale_x_continuous(breaks = seq(from = 50, to = 60, by = 2))

# print g1, and then add g2 on top using viewport from package grid
g1
print(g2, vp = viewport(x = 0.75, y = 0.3, width = 0.35, height = 0.35))

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • It seems this the only way !!! how about if we make the ticks from 100 to 200 increase by 50. Would that give more space to show the labels of the tiny ticks !!! – SimpleNEasy Oct 07 '13 at 00:00
  • Can you please clarify exactly what you want to achieve. How would the tickmarks at 100 to 200 increase the space between 50 and 60? You have the number of pixels you have. If something very special happens in the interval 50-60 I would rather consider making a subplot with that section zoomed in. However, nobody can guess what you want. [Please be specific](http://meta.stackoverflow.com/help/how-to-ask). – Henrik Oct 07 '13 at 00:17
  • Yes, you are right, something special happening in the interval 50-60 and I 'd like to show clearly in the graph. I thought reducing the ticks between 100-200 would help not to squeeze the interval 50-60. All I want to show that interval 50-60 with minor label ticks. I think the only way is to do your suggestion. So, how to make a subplot with that section zoomed in ? – SimpleNEasy Oct 07 '13 at 01:33
  • how do we get rid of the space between the start of the graph (origin) and the 0 in the graph above. Is it related to environment() ?? – SimpleNEasy Oct 23 '13 at 20:22
  • @SimpleNEasy, try to add `scale_x_continuous(expand = c(0, 0))` when you create the `g1` plot. – Henrik Oct 23 '13 at 20:31
  • Great! Just curious: does your plot with c(0, 0.2) differ from c(0, 0)?. See [here](http://docs.ggplot2.org/current/continuous_scale.html): "expand: a numeric vector of length two, giving a multiplicative and additive constant used to expand the range of the scales so that there is a small gap between the data and the axes.". I am not not sure what the result is if the the numbers are 'in conflict' with each other (e.g. 0 vs 0.2). Well, well, as long as you are happy with your plot! – Henrik Oct 23 '13 at 20:43
  • it doesn't make a notable difference almost like expand=c(0,0). Was thinking to add small space out of curiosity. – SimpleNEasy Oct 23 '13 at 20:50