2

I am just trying to plot a small 27 point dataset with X-axis as Date (CommonDate) in date R format, and y-axis as a continuous variable.

Despite having the date variable in Date format, and even after commenting out the part where scale_x_date() is being utilized, I am getting this error. I have tried multiple ways of debugging it based on some of the answers to this particular error, but to no avail.

Sample dataset: I have sample a 12 point dataset out of the 27 point one

    > example
     Brand     Period X2014_2013 X2015_2014 CommonDate Year
6        B 2014-06-01  -9.168405 -19.508786 2000-05-31 2014
11       B 2014-11-01  33.747651   0.000000 2000-10-31 2014
19       B 2014-07-01  50.199601   0.000000 2000-06-30 2014
20       B 2014-08-01  69.084423   0.000000 2000-07-31 2014
22       B 2014-10-01 130.377668   0.000000 2000-09-30 2014
3        B 2014-03-01  29.807692   9.856387 2000-02-29 2014
NA    <NA>       <NA>         NA         NA       <NA>   NA
NA.1  <NA>       <NA>         NA         NA       <NA>   NA
2        B 2014-02-01  21.843116 -13.037997 2000-02-01 2014
16       B 2014-04-01 601.443299  43.298060 2000-03-31 2014
5        B 2014-05-01  15.477101 -23.492664 2000-04-30 2014
23       B 2014-11-01 126.591315   0.000000 2000-10-31 2014
     Month
6        6
11      11
19       7
20       8
22      10
3        3
NA      NA
NA.1    NA
2        2
16       4
5        5
23      11

This is the code I am running. Here instead of data = perc_change_YOY_abs, you may use data = example

newb_brand_abs_perc_YOY_2015 = 
ggplot(data = perc_change_YOY_abs, aes(x = CommonDate, y = X2015_2014, colour = Brand))+
geom_point()+
geom_line()+
geom_text(aes(label = paste(round(X2015_2014,0), "%"), hjust = 0, vjust = 1), size = 4)+
scale_x_date(breaks = date_breaks("months"),labels = date_format("%b"))+
scale_y_continuous(breaks = pretty_breaks(n = 8))+
theme_bw()+
xlab(" ")+
ylab("%Change 2015 vs. 2014")+
annotate("text", x = 4, y = 25, label = "Some text")

plot(newb_brand_abs_perc_YOY_2015)

Sessioninfo:

> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets 
[7] methods   base     

other attached packages:
[1] lubridate_1.3.3 reshape2_1.4.1  plyr_1.8.3     
[4] gridExtra_0.9.1 scales_0.2.5    lattice_0.20-31
[7] ggplot2_1.0.1  

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.6      digest_0.6.8     MASS_7.3-40     
 [4] gtable_0.1.2     magrittr_1.5     stringi_0.5-4   
 [7] proto_0.3-10     tools_3.2.0      stringr_1.0.0   
[10] munsell_0.4.2    colorspace_1.2-6 memoise_0.2.1
  • If that's a small dataset, make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by `dput`ting it. – tonytonov Jul 06 '15 at 15:35

3 Answers3

1

So rather than using the annotate() function within ggplot, you could use geom_text().

You first have to make a data table or frame with your labels

x <- as.Date(c('2000-07-24'))
y <- c(30)
labs <- 'Some Text'
labelData <- data.table(x = x, y = y, labs = labs)

It should look something like this

            x  y      labs
1: 2000-07-24 30 Some Text

Notice how the x column is a Date type.

Then in your ggplot call, replace

annotate("text", x = 4, y = 25, label = "Some text")

with

geom_text(aes(x, y, label = labs, group = NULL), data = labelData)

That should solve your problem! I think annotate was having trouble interpreting the x coordinate into a Date type, but geom_text handles it nicely.

Alexander Heath
  • 116
  • 1
  • 4
  • As you suggested, I converted the date variable into a factor. However, although I get rid of the `date_trans` error I was encountering earlier, I do not fulfill my purpose of plotting `geom_line()` dates vs. value. Can you suggest how I can get my plot without running into the error? – Angad Gadre Jul 06 '15 at 17:43
  • I edited my post above. Did this solve your question? – Alexander Heath Jul 07 '15 at 16:18
1

Here's another solution if you want to stick with the annotate command.

annotate("text", x=as.Date("2014-04-01"), y=25, label="some text")

This other StackOverflow question provides another example. Difficulty annotating plot when x axis values are dates

Community
  • 1
  • 1
John J.
  • 1,450
  • 1
  • 13
  • 28
0

I tried the annotate("text", x=as.Date("2014-04-01"), y=25, label="some text") solution by John J., but it didn't work for me (date_trans error), but I found a solution here. The idea is to use as.POSIXct():

 annotate("text", x = as.POSIXct(-Inf, origin = '1970-01-01'), y = Inf, 
                       hjust = 0, vjust = 1, label = "Hello world!") 
RobertMyles
  • 2,673
  • 3
  • 30
  • 45