[R] Shading the plot

R. Michael Weylandt michael.weylandt at gmail.com
Mon Dec 5 23:11:59 CET 2011


As David said, your "minimal working example" is neither minimal nor
working...consider this (admittedly quite clunky) example though:

time <- seq(10, 20, length.out = 100)

S1 <- 10*exp( cumsum(rnorm(100))/20)
S2 <- 10*exp( cumsum(rnorm(100))/20)

plot(time, S1, ylim = range(c(S1,S2)), type = "n")
lines(time, S1); lines(time, S2, col = 2)

r <- c(1, which(diff(sign(S1-S2)) != 0L), length(time))

for(i in seq_along(r)){
    if(i == length(r)){next}

    ind1 <- r[i]
    ind2 <- r[i+1]

    Xbnds <- time[ind1:ind2]
    Xbnds <- c(Xbnds, rev(Xbnds))

    Ybnds <- c(S1[ind1:ind2],S2[ind2:ind1])

    col = if(median((S1>S2)[ind1:ind2])) "blue" else "green"

    polygon(Xbnds, Ybnds, col = col, border = NA)

}

Best I can figure, this fills green if the red line is on top and blue
otherwise. You may have to run it a few times to see that behavior.

Michael

On Sat, Dec 3, 2011 at 11:37 PM, avinash barnwal
<avinashbarnwal123 at gmail.com> wrote:
> Hi,
>
> I apologies  for my naive doubts
> I have been trying it from a while. I need the area between the curve based
> on conditions i wrote (whenever red line is above the blue line then area
> between curves to be grey color and whenever blue line is above red line
> then area between curves to be red color )
>
> x and y are vectors
>
> Thank you for replying
>
>
>
>
> On Sun, Dec 4, 2011 at 7:56 AM, David Winsemius <dwinsemius at comcast.net>
> wrote:
>>
>>
>> On Dec 3, 2011, at 5:28 PM, avinash barnwal wrote:
>>
>>> Hi Weylandt,
>>>
>>>
>>> I tried it but i was not successful.
>>>
>>> Here is the full code
>>>
>>> Any help would be great.
>>> ########################################################################
>>>
>>> time<-c("02-Jan-05","09-Jan-05","16-Jan-05","23-Jan-05","30-Jan-05","06-Feb-05","13-Feb-05","20-Feb-05","27-Feb-05","06-Mar-05","13-Mar-05",
>>>
>>> "20-Mar-05","27-Mar-05","03-Apr-05","10-Apr-05","17-Apr-05","24-Apr-05","01-May-05","08-May-05","15-May-05")
>>> time<-as.Date(time,"%d-%b-%y")
>>>
>>> a2<-c("81.96392786","81.96392786","82.16432866","82.56513026","82.76553106","79.96593186","78.16633267",
>>>
>>> "75.56713427","74.76753507","72.96793587","70.96793587","45.96793587","83.96793587","84.36873747",
>>>
>>> "84.56913828","84.56913828","84.56913828","84.56913828","84.36873747","84.36873747")
>>>
>>> a3<-c("81.96392786","81.96392786","81.96392786","70.96392786","68.16432866","66.16432866",
>>>
>>> "62.16432866","58.56513026","56.56513026","54.56513026","48.56513026","49.76553106","52.76553106",
>>>
>>> "54.76553106","57.96593186","59.36673347","83.36673347","83.36673347","83.36673347",
>>> "83.56713427")
>>> j<-0
>>> for(i in 1:length(a3))
>>> {
>>> if(a2[i]>a3[i])
>>> {
>>> j<-j+1
>>> x[j]<-a2[i]
>>> y[j]<-a3[i]
>>> temp_time[j]<-time[i]
>>> }
>>> }
>>> plot(time,a2,type='l',col='red',ylab='',xlab=" ",axes=FALSE)
>>> lines(time,a3,col='blue',ylab='',xlab=" ")
>>> polygon(c(temp_time,rev(temp_time)),c(x,y),col="grey")
>>> #########################################################################
>>
>>
>> What are the rules that govern requesting outside help in homework at your
>> institution?
>>
>> Your current code does not recognize  that there is line crossing. Nor do
>> you describe what you mean by shading. If you wanted a rectangle within the
>> lpot area it would drive one strategy and if you wanted only the area
>> between the curves to be shaded it would drive a different strategy.
>>
>> AND you never define "x" or "y".
>>
>> --
>> David.
>>
>>
>>
>>> On Sun, Dec 4, 2011 at 2:46 AM, R. Michael Weylandt <
>>> michael.weylandt at gmail.com> wrote:
>>>
>>>> ? polygon
>>>> example(polygon)
>>>>
>>>> Michael
>>>>
>>>> On Sat, Dec 3, 2011 at 4:08 PM, avinash barnwal
>>>> <avinashbarnwal123 at gmail.com> wrote:
>>>>>
>>>>> Hi all,
>>>>>
>>>>> I have been trying to shade the specific part of the plot
>>>>>
>>>>> Part to be shaded
>>>>>
>>>>> 1. Any color whenever a2>a3
>>>>>
>>>>> 2. Any other color( Not same as 1) whenever a2<a3
>>>>>
>>>>> Suggest me some code for this task
>>>>>
>>>>> PLOT CODE for reference
>>>>>
>>>>>
>>>>
>>>> ######################################################################################################
>>>>>
>>>>>
>>>>
>>>> a2<-c("81.96392786","81.96392786","82.16432866","82.56513026","82.76553106","79.96593186","78.16633267",
>>>>>
>>>>>
>>>>
>>>> "75.56713427","74.76753507","72.96793587","70.96793587","45.96793587","83.96793587","84.36873747",
>>>>>
>>>>>
>>>>
>>>> "84.56913828","84.56913828","84.56913828","84.56913828","84.36873747","84.36873747")
>>>>>
>>>>>
>>>>
>>>> a3<-c("81.96392786","81.96392786","81.96392786","70.96392786","68.16432866","66.16432866",
>>>>>
>>>>>
>>>>
>>>> "62.16432866","58.56513026","56.56513026","54.56513026","48.56513026","49.76553106","52.76553106",
>>>>>
>>>>>
>>>>
>>>> "54.76553106","57.96593186","59.36673347","83.36673347","83.36673347","83.36673347",
>>>>>
>>>>> "83.56713427")
>>>>> plot(a2,type='l',col='red',ylab='',xlab=" ",axes=FALSE)
>>>>> lines(a3,col='blue',ylab='',xlab=" ")
>>>>>
>>>>
>>>> ######################################################################################################
>>>>>
>>>>>
>>>>> Thank you in the advance
>>>>> --
>>>>> Avinash Barnwal
>>>>> Final year undergraduate student
>>>>> Statistics and informatics
>>>>> Department of Mathematics
>>>>> IIT Kharagpur
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> 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.
>>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Avinash Barnwal
>>> Final year undergraduate student
>>> Statistics and informatics
>>> Department of Mathematics
>>> IIT Kharagpur
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> 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.
>>
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>
>
>
> --
> Avinash Barnwal
> Final year undergraduate student
> Statistics and informatics
> Department of Mathematics
> IIT Kharagpur



More information about the R-help mailing list