[R] Polygon shaded area

R. Michael Weylandt michael.weylandt at gmail.com
Fri Aug 3 01:18:39 CEST 2012


On Thu, Aug 2, 2012 at 5:53 PM, Jose Narillos de Santos
<narillosdesantos at gmail.com> wrote:
> Many thanks Eik it works properly.
>
> what I don´t know exactly is why you put
> c(xv,rev(xv)),c(efinal,rev(efinal2)),col="red" inside polygon. I see it
> works but for the moment I don´t really see the meaning of c() and why is
> needed to put twice and so on.

c() is for combining vectors [think "concatenate"] and the reason you
have to combine xy with xy-reversed is to draw both sides of the
polygon: imagine I asked you to draw a square without raising your
pencil. However you do it, you'll go opposite directions along the top
and the bottom. Same idea leads to the rev() inside polygon.


>
> If I had a vector of dates associated with each row (imagine t.csv)
>
> t<-read.csv("t.csv")
>
> tp<-as.Date(t[,2], format="%d/%m/%Y")
>
> I see that if I put
>
> plot(efinal, tp. ylim=range(c(efinal,efinal2)),type="n",ylab="")
> xv<-seq_along(efinal)
> polygon(c(xv,rev(xv)),c(efinal,rev(efinal2)),col="red")
>
>

Maybe this helps:

tframe <- data.frame(dat =  1:10,  day = Sys.Date() + 1:10)
# t is a bad variable name because there's a t = transpose function
which is really important

plot(dat ~ day, data = tframe)

with(tframe, polygon(c(day, rev(day)) + 0, c(dat, rep(0, length.out =
length(dat))), col = "blue"))

Best,
Michael

> It doesn´t work.
>
> I can use xaxt="n" but it will be ok to put associated "date" on x axis.
>
> Sorry in advance if I only ask but it is difficoult to me, anyway many
> thanks and I will keep on trying. Many people (with some original reason)
> get ennoyed for some silly questions, so I send many thanks for read me and
> try to help me.
>
> Many, many thanks.
>



More information about the R-help mailing list