Re: [R] Filled step-function?

From: Petr Klasterecky <klaster_at_karlin.mff.cuni.cz>
Date: Wed, 16 May 2007 14:19:57 +0200

Hi,

please reply to the entire list (as a carbon copy) - I do not always read this list properly or may not know the solution.

I do not know your data, but there does not seem to be much more work. This works well for me with your pH and Tiefe variables:

plot(pH,Tiefe,type="s",main="Tiefenfunktion pH",ylim=c(120,0),xlim=c(0,23)) for (i in (1:(length(pH)-2))){
polygon(x=c(pH[i], pH[i+1], pH[i+1], pH[i]), y=c(Tiefe[i], Tiefe[i], max(Tiefe), max(Tiefe)), col=i)
}

or

plot(pH,Tiefe,type="s",main="Tiefenfunktion pH",ylim=c(120,0),xlim=c(0,23)) for (i in (1:(length(pH)-2))){
polygon(x=c(pH[i], pH[i+1], pH[i+1], pH[i]), y=c(Tiefe[i], Tiefe[i], Tiefe[i+1], Tiefe[i+1]), col=i)
}

You just need to arrange your coordinates properly. If your variables are not always named pH and Tiefe, just access them directly from the labdata data.frame: you will have labdata[,1] for variable1 (here pH) and labdata[,2] for variable2 (Tiefe).

hth
Petr

Beate Kowalczyk napsal(a):
> Hi Peter,
>
> polygon doesn't work very well, because I need a graphical display for
> the laboratory results of my institute. So I have a lot of data in
> .csv-format, and I don't really want to define a new polygon for each
> dataset, because then I have to do a lot of extra-work. I need a
> solution that works like the following, but with coloring the area under
> the function (like the image in my first posting):
>
> labdata <- read.csv("labdata.csv", header=TRUE)
> attach(labdata) #for using column-names as variables
> plot(variable1,variable2,type="s",main="Title",ylim=c(120,0),xlim=c(0,10))
>
> For each .csv-dataset I want to use this code with only slight
> adjustments...
>
> Tanks again, Beate
>
> Petr Klasterecky schrieb:

>> Hi,
>>
>> I am not sure what exactly you would like to fill up and how, but take 
>> a look at
>> ?polygon
>> and the examples given there.
>>
>> hth
>> Petr
>>
>>
>> Beate Kowalczyk napsal(a):
>>  
>>> Hello,
>>>
>>> I'd like to generate a filled step-function in R as you can see in 
>>> the attachement (image is generated by grapher, but I need a freely 
>>> available alternative). The problem is, that I don't know, how to 
>>> fill it up. My code right now looks like this:
>>>
>>> pH <- c(0,6.1,6.1,6.3,6.6,7.3,0)
>>> Tiefe <- c(0,0,10,40,80,120,10)
>>> plot(pH,Tiefe,type="s",main="Tiefenfunktion 
>>> pH",ylim=c(120,0),xlim=c(0,23))
>>>
>>> Can someone give me an hint how to work on this?
>>>
>>> Thanks, Beate
>>>
>>>
>>> ------------------------------------------------------------------------
>>>
>>>
>>> ------------------------------------------------------------------------
>>>
>>> ______________________________________________
>>> R-help_at_stat.math.ethz.ch 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.
>>>     
>>
>>   

>
>
-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic

______________________________________________
R-help_at_stat.math.ethz.ch 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.
Received on Wed 16 May 2007 - 12:25:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 16 May 2007 - 12:31:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.