[R] Plot Stepped Line chart with multiple lines

David Winsemius dwinsemius at comcast.net
Wed Feb 23 02:08:38 CET 2011


On Feb 22, 2011, at 7:10 PM, Ista Zahn wrote:

> Hi,
> This is R, so there are bound to be severay ways to do it. This would
> be my first choice:
>
> library(ggplot2)
> ggplot(d, aes(x=V2, y=V3, color=V1)) + geom_step()
>
> Best,
> Ista
>
> On Tue, Feb 22, 2011 at 3:08 PM, Techni X <fibosworld at yahoo.com>  
> wrote:
>> Hi all,
>>
>> I have a question, that might be a „rookie“ question – but I’m  
>> trying now for days and cannot get my head around. The general  
>> question is:
>> How can I plot a stepped line chart with multiple lines from a  
>> subset of a dataframe?
>>
>> An example:
>> d <- matrix(rep(0,24), ncol=3, nrow=8)
>> d <- as.data.frame(d)
>> d$V1 <- c("A","A","A","B","B","C","C","C")
>> d$V2 <- c(5,10,15,10,15,5,13,15)
>> d$V3 <- c(10,20,30,7.5,10,3,7,10)

Probably the second to these:

require(lattice)
xyplot(V3 ~ V2|V1, data=d, type="s")  #separate panels
xyplot(V3 ~ V2, group=V1, data=d, type="s")  # all on one panel

>> which gives me the following dataframe:
>>  V1 V2   V3
>> 1  A  5 10.0
>> 2  A 10 20.0
>> 3  A 15 30.0
>> 4  B 10  7.5
>> 5  B 15 10.0
>> 6  C  5  3.0
>> 7  C 13  7.0
>> 8  C 15 10.0
>>
>> I now want to plot three stepped lines into a x-y chart, the lines  
>> representing A, B, C. The x-axis is given by V2, the y-axis by V3,  
>> thus each line will be “ascending”
>> It must be something with SUBSET or so, but I do not find an easy  
>> way.
>>
>> Can anybody help me?
>>
>> Thanks
>> Stephan


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list