[R] Filled step-function?

Petr Klasterecky klaster at karlin.mff.cuni.cz
Wed May 16 14:19:57 CEST 2007


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



More information about the R-help mailing list