10
votes

I am plotting value~date in ggplot2 (in R). I have the following code. As you see ggplot2 adds more breaks on the x-axis that I have in my data. I just want to have the x-label everytime I have a data point in my data frame. How can I force ggplot2 to just show the breaks only at the values of my.dates? It seems there is no "breaks" argument for scale_x_date

require(ggplot2)
my.dates = as.Date(c("2011-07-22","2011-07-23",
                     "2011-07-24","2011-07-28","2011-07-29"))
my.vals  = c(5,6,8,7,3)
my.data <- data.frame(date =my.dates, vals = my.vals)
plot(my.dates, my.vals)
p <- ggplot(data = my.data, aes(date,vals))+ geom_line(size = 1.5)
p <- p + scale_x_date(format="%m/%d", ' ')
p

enter image description here

1
See also scale_x_date(): "The date scale will attempt to pick sensible defaults for major and minor tick marks. Override with date_breaks, date_labels, date_minor_breaks arguments."Paul Rougieux

1 Answers

14
votes

One approach would be to treat the x-axis as numeric and set the breaks and labels aesthetics with scale_x_continuous().

ggplot(my.data, aes(as.numeric(date), vals)) + 
  geom_line(size = 1.5) +
  scale_x_continuous(breaks = as.numeric(my.data$date)
                     , labels = format(my.data$date, format = "%m/%d"))

though the break between 7/24 through 7/28 looks a bit strange in my opinion. However, I think that's what you want? Let me know if I've misinterpreted.

EDIT

As noted above, I wasn't thrilled with the way the breaks looked above, specifically with the gray grid in the background. Here's one way to maintain the rectangular grid and to only label the points where we have data. You could do this all within the ggplot call, but I think it's easier to do the processing outside of ggplot. First, create a vector that contains the sequence of numbers corresponding to the dates. Then we'll update the appropriate labels and replace the NA entries with " " to prevent anything from being plotted on the x-axis for those entries:

xscale <- data.frame(breaks = seq(min(as.numeric(my.data$date)), max(as.numeric(my.data$date)))
                      , labels = NA)

xscale$labels[xscale$breaks %in% as.numeric(my.data$date)] <- format(my.data$date, format = "%m/%d") 
xscale$labels[is.na(xscale$labels)] <- " "

This gives us something that looks like:

  breaks labels
1  15177  07/22
2  15178  07/23
3  15179  07/24
4  15180       
5  15181       
6  15182       
7  15183  07/28
8  15184  07/29

which can then be passed to the scale like this:

scale_x_continuous(breaks = xscale$breaks, labels = xscale$labels)