1
votes

I am trying to highlight a time series multiple times with the package ggplot2, I created a data.frame with the starting dates (xmin) and the ending dates (xmax) of all the highlights, but I'm getting: Error: Aesthetics must either be length one, or the same length as the dataProblems:xmin, xmax, ymin, ymax

the code is:

g.bottom <- ggplot(,aes(x =decimal_date(date), y =C$PE)) +
geom_rect(data=s,aes(xmin=decimal_date(xmin), xmax=decimal_date(xmax),ymin=ymin,ymax=ymax),
alpha=0.1, color="pink", fill="pink")+
geom_line() +
theme_classic() +
theme(plot.margin = unit(c(0,5,10,4),units="points"))+
labs(x="Fecha",y = "PE")

this is the data s:

xmin<-c("2011/07/28","2012/04/09","2012/04/16","2013/04/22","2014/12/08","2014/12/15","2014/12/18")
xmax<-c("2011/08/08","2012/04/12","2012/06/12","2013/04/26","2014/12/11","2014/12/16","2015/04/09")
ymin<- c(-Inf,-Inf,-Inf,-Inf,-Inf,-Inf,-Inf)
ymax<-c(Inf,Inf,Inf,Inf,Inf,Inf,Inf)
s<- data.frame(xmin,xmax,ymin,ymax)
transform(s, xmin=as.Date(xmin,format="%Y/%m/%d"))%>%
transform(xmax=as.Date(xmax,format="%Y/%m/%d"))->s
s
        xmin       xmax     ymin ymax
    1 2011-07-28 2011-08-08 -Inf  Inf
    2 2012-04-09 2012-04-12 -Inf  Inf
    3 2012-04-16 2012-06-12 -Inf  Inf
    4 2013-04-22 2013-04-26 -Inf  Inf
    5 2014-12-08 2014-12-11 -Inf  Inf
    6 2014-12-15 2014-12-16 -Inf  Inf
    7 2014-12-18 2015-04-09 -Inf  Inf

and this is C

      Date     GSADF    PE
1 26/01/2011 0.7990547 29.07
2 27/01/2011 0.7970526 29.20
3 28/01/2011 0.7947446 28.66
4 31/01/2011 0.7950117 29.05
5 01/02/2011 0.7931063 29.72
6 02/02/2011 0.7900929 29.92
1
Could you provide your data, C as well?jazzurro
your code works fine once all the data are of the right type. Did you make sure xmin and xmax are in the right format? library(lubridate) ; s$xmin <- ymd(s$xmin) ; s$xmax <- ymd(s$xmax)scoa
I head C for you @jazzurro, and also did what scoa suggested bud didn't work, I am getting the same ErrorAlejandro Andrade

1 Answers

0
votes

Here is one approach for you. I think the key is to use decimal_date(). You can use the function and arrange your data frame before you write your ggplot codes. Here, I used the function in one of the lines for ggplot. I hope this will help you.

library(lubridate)
library(magrittr)
library(ggplot2)

mydf <- read.table(text = "      Date     GSADF    PE
1 26/01/2011 0.7990547 29.07
2 27/01/2011 0.7970526 29.20
3 28/01/2011 0.7947446 28.66
4 31/01/2011 0.7950117 29.05
5 01/02/2011 0.7931063 29.72
6 02/02/2011 0.7900929 29.92", header = T)

### Convert factor to date
transform(mydf, Date = as.Date(Date, format = "%d/%m/%Y")) -> mydf

mydf2 <- read.table(text = "     xmin       xmax     ymin ymax
1 2011-07-28 2011-08-08 -Inf  Inf
2 2012-04-09 2012-04-12 -Inf  Inf
3 2012-04-16 2012-06-12 -Inf  Inf
4 2013-04-22 2013-04-26 -Inf  Inf
5 2014-12-08 2014-12-11 -Inf  Inf
6 2014-12-15 2014-12-16 -Inf  Inf
7 2014-12-18 2015-04-09 -Inf  Inf", header = T)

### Convert factor to date
transform(mydf2, xmin = as.Date(xmin, format = "%Y-%m-%d")) %>%
transform(xmax = as.Date(xmax, format = "%Y-%m-%d")) -> mydf2


### Decial dates are necessary. Use decimal_date() in the lubridate package.

ggplot() + 
geom_rect(data = mydf2, aes(xmin = decimal_date(xmin), xmax = decimal_date(xmax), ymin = ymin, ymax = ymax),
      alpha = 0.1, color = "pink", fill = "pink") +
geom_line(data = mydf, aes(x = decimal_date(Date), y = PE))

enter image description here