Shaded areas in R

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Shaded areas in R

Óscar Jiménez
Hello,

I'm working with R language, and plotting some parameters over time. I need
to draw a shaded area under the curve of eacj parameter.

For that, I might use the polygon (x,y) function, assigning coordinates
(x,y) to each vertex of my polygon. To do so, "x" and "y" must be vectors
with numerical values, but since my x-axis is a time series, I cannot
assing a numerical value to my "x" coordinate, because time variable is a
"character" variable.

Is there any option to use the function polygin (x,y) in this case, or any
other function that allows me to draw a shaded area under the curve on a
time series basis?

Thank you in advance for your help

Best regards

Óscar

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Shaded areas in R

Tom Wright-9
Hi Óscar,

Not really sure what you mean by the time variable being a "character"
vector. Unless you are plotting a barchart or boxplot (or similar), I
don't this this makes sense. If you can post a sample of your data,
preferably using the dput() command we can probably help more.

On Thu, May 26, 2016 at 5:37 AM, Óscar Jiménez
<[hidden email]> wrote:

> Hello,
>
> I'm working with R language, and plotting some parameters over time. I need
> to draw a shaded area under the curve of eacj parameter.
>
> For that, I might use the polygon (x,y) function, assigning coordinates
> (x,y) to each vertex of my polygon. To do so, "x" and "y" must be vectors
> with numerical values, but since my x-axis is a time series, I cannot
> assing a numerical value to my "x" coordinate, because time variable is a
> "character" variable.
>
> Is there any option to use the function polygin (x,y) in this case, or any
> other function that allows me to draw a shaded area under the curve on a
> time series basis?
>
> Thank you in advance for your help
>
> Best regards
>
> Óscar
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Shaded areas in R

Duncan Murdoch-2
In reply to this post by Óscar Jiménez
On 26/05/2016 5:37 AM, Óscar Jiménez wrote:

> Hello,
>
> I'm working with R language, and plotting some parameters over time. I need
> to draw a shaded area under the curve of eacj parameter.
>
> For that, I might use the polygon (x,y) function, assigning coordinates
> (x,y) to each vertex of my polygon. To do so, "x" and "y" must be vectors
> with numerical values, but since my x-axis is a time series, I cannot
> assing a numerical value to my "x" coordinate, because time variable is a
> "character" variable.
>
> Is there any option to use the function polygin (x,y) in this case, or any
> other function that allows me to draw a shaded area under the curve on a
> time series basis?

Times and dates just print like characters, they aren't actually
characters.  For example,

  x <- Sys.Date() + 1:20
  y <- rnorm(20)
  plot(y ~ x)
  polygon(c(x, x[20], x[1]), c(y, 0, 0), col="gray")

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Shaded areas in R

Duncan Murdoch-2
On 26/05/2016 11:03 AM, Óscar Jiménez wrote:
> Hi Duncan,
>
> Thanks for the quick reply  :)
> Does the function Sys.Date return a time series (created with the
> function POSIXct), with numerical values?

It returns a Date object.  The str() function will show you that. But
Date, POSIXct, and even POSIXlt are supported by many of the graphics
functions, so you don't need to worry about the conversions.

> I mean... I think the best option is to convert the time series
> (plotted as characters), into numerical values, right?

No, that is not needed.

> Or is there any other function that allows me to draw the shade under
> the curve using time series as the "x" variable?

polygon() does.

You should try things; R won't break.
>
> If you need more info I can share my script and my database and you
> can see it properly.

You should always do that when you pose your question; you should also
send your questions to R-help, not privately.  I've cc'd my response there.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Fortune candidate, was Re: Shaded areas in R

Michael Dewey-3
In a reply Duncan said

On 26/05/2016 16:12, Duncan Murdoch wrote:
> On 26/05/2016 11:03 AM, Óscar Jiménez wrote:
>
> You should try things; R won't break.
>>
>

> Duncan Murdoch
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Michael
http://www.dewey.myzen.co.uk/home.html

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Shaded areas in R

Óscar Jiménez
In reply to this post by Duncan Murdoch-2
Thanks for your answers

I'm going to share my script and my database in an excel file, where you
can check the kind of graph I would like to make (see attached document,
you can find it in Sheet1. Sheet 3 is the one that I used in .csv format to
work with in R).

Script:

# HYDROGRAPH SEPARATION ANALYSIS FOR EVENTS: JULY 9TH AND SEPT 11TH

# Event 20. July 9th, 2015

setwd("C:\\Users\\Asus\\Desktop\\MSc Thesis - INIRENA\\R\\Downloads")
dir()

  # Ev20HS: P event 20. Hydrograph separation
Ev20HS <- read.table("Event20_Hydrograph_Separation.txt", skip=4,
header=FALSE, stringsAsFactors=FALSE)
as.data.frame(Ev20HP)
colnames(Ev20HS)<- c("Date","Hour","P", "Qt", "St2H", "P2H", "GW2H",
"St18O", "P18O", "GW18O",
                     "Qold2H", "Qnew2H", "%old2H", "%new2H", "Qold18O",
"Qnew18O", "%old18O", "%new18O")
paste(Ev20HS$Date, Ev20HS$Hour)
Ev20HS$date.time <- paste(Ev20HS$Date, Ev20HS$Hour)
as.POSIXct(Ev20HS$date.time, format="%Y/%m/%d %H:%M:%S", tz="-7")
Ev20HS$date.time <- as.POSIXct(Ev20HS$date.time, format="%Y/%m/%d
%H:%M:%S", tz="-7")

par(mar=c(4, 4, 4, 4))
plot(Ev20HS$date.time, Ev20HS$P, type="h", axes= FALSE, col="cadetblue4",
ylim=c(10,0),
     lwd=2, xlab= "Hour", xline= 1.5, ylab = " ")
axis (4, lwd= 1)
mtext (4, line=2, text="P (mm)")
par(new=T)
plot(Ev20HS$date.time, Ev20HS$Qt, type="l", col="deepskyblue", lwd=2,
ylim=c(0,0.02), xlab= " ", ylab = " ")
axis (2, lwd= 1)
mtext (2, line=2.25, text="Q (mm/10min)")
par(new=T)
plot(Ev20HS$date.time, Ev20HS$Qold2H, type="l", axes= FALSE, col="sienna3",
lwd=2, ylim=c(0,0.02), xlab= " ", ylab = " ")
cord.x <- c(7, 7, 13, 13)
cord.y <- c(0.00223742, 0.00238599, 0.01043445, 0.00970288)
polygon(cord.x, cord.y, col="steelblue3")


I want to plot in the same graph:

a) Precipitation (P) as a barplot (downwards) --> DONE
b) Total discharge in Stream (Qt) --> DONE
c) Discharge of groundwater, i.e. old component (Qold2H) --> The space
between Qt and Qold2H needs to be shaded with one colour, and the space
between Qold2H and x-axes needs to be shaded as well.


Another question, is there any option to make these curves smoother? I
don't really like those ugly peaks.

On 26 May 2016 at 17:21, Duncan Murdoch <[hidden email]> wrote:

> On 26/05/2016 11:20 AM, Óscar Jiménez wrote:
>
>> oops, sorry I didn't know that... I'll answer in R-help. It was the 1st
>> time I was using this tool.
>>
>> Also, I never learned programming and R... so I do not control theses
>> languages. I'm just trying to work with it and got no support, and so many
>> questions arises.. hahaha
>>
>
> Don't worry about it.  I was just giving advice for the future.
>
> Duncan Murdoch
>
>
>
>> On 26 May 2016 at 17:12, Duncan Murdoch <[hidden email]
>> <mailto:[hidden email]>> wrote:
>>
>>     On 26/05/2016 11:03 AM, Óscar Jiménez wrote:
>>
>>         Hi Duncan,
>>
>>         Thanks for the quick reply  :)
>>         Does the function Sys.Date return a time series (created with
>>         the function POSIXct), with numerical values?
>>
>>
>>     It returns a Date object.  The str() function will show you that.
>>     But Date, POSIXct, and even POSIXlt are supported by many of the
>>     graphics functions, so you don't need to worry about the conversions.
>>
>>         I mean... I think the best option is to convert the time
>>         series (plotted as characters), into numerical values, right?
>>
>>
>>     No, that is not needed.
>>
>>         Or is there any other function that allows me to draw the
>>         shade under the curve using time series as the "x" variable?
>>
>>
>>     polygon() does.
>>
>>     You should try things; R won't break.
>>
>>
>>         If you need more info I can share my script and my database
>>         and you can see it properly.
>>
>>
>>     You should always do that when you pose your question; you should
>>     also send your questions to R-help, not privately. I've cc'd my
>>     response there.
>>
>>     Duncan Murdoch
>>
>>
>>
>
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Fortune candidate, was Re: Shaded areas in R

Achim Zeileis-4
In reply to this post by Michael Dewey-3
On Thu, 26 May 2016, Michael Dewey wrote:

> In a reply Duncan said

Nice, thanks, added to the "fortunes" package on R-Forge now.
Best,
Z

> On 26/05/2016 16:12, Duncan Murdoch wrote:
>> On 26/05/2016 11:03 AM, Óscar Jiménez wrote:
>>
>> You should try things; R won't break.
>>>
>>
>
>> Duncan Murdoch
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Michael
> http://www.dewey.myzen.co.uk/home.html
>
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.