Re: Plot 3 lines in one graph

96 views
Skip to first unread message

Ignacio Martinez

unread,
Nov 6, 2012, 8:33:54 AM11/6/12
to ggp...@googlegroups.com
I found a method to do what I wanted :-)

y<-reshape(x, varying = c("FT", "PT", "NW"), 
           v.names = "Value", timevar = "h", 
           times = c("FT", "PT", "NW"), 
           new.row.names = 1:1000, 
           direction = "long")

if there is a better way of doing it please let me know.

Thanks!


On Mon, Nov 5, 2012 at 10:11 PM, ignacio <igna...@gmail.com> wrote:
I'm new with R. I want to plot 3 lines in one graph. This is my data:
print(x)
     V1       V2       V3      V4
1 -4800 25195.73 7415.219 7264.28
2 -2800 15195.73 5415.219 7264.28
x$V1 Is the vector of coordinates for the horizontal axis and V2, V3, V4 the vectors for the vertical.
I would like to make the lines extend so I can see the intersections.

From what I understand from this example I would need to reshape my data to something like this

     id       x       y      
1     1     -4800   25195.73 
2 1 -2800 15195.73 3 2 -4800 7415.219 4 2 -2800 5415.219 5 3 -4800 7264.28 6 3 -2800 7264.28

Is this right? Can somebody explain me how to do it?
Thanks a lot for the help!


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

ONKELINX, Thierry

unread,
Nov 6, 2012, 8:49:15 AM11/6/12
to Ignacio Martinez, ggp...@googlegroups.com

Yes. Use melt() from the reshape package.

 

melt(x, id.var = “h”, measure.var = c(“FT”, “PT”, “NW”))

 

or if x contains only those 4 variables

 

melt(x, id.var = “h”)

 

 

ir. Thierry Onkelinx

Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest

team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance

Kliniekstraat 25

1070 Anderlecht

Belgium

+ 32 2 525 02 51

+ 32 54 43 61 85

Thierry....@inbo.be

www.inbo.be

 

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.

~ Sir Ronald Aylmer Fisher

 

The plural of anecdote is not data.

~ Roger Brinner

 

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.

~ John Tukey

* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.

Ben Bond-Lamberty

unread,
Nov 8, 2012, 11:10:43 AM11/8/12
to ggp...@googlegroups.com
Jorge,

Perhaps start by working through a few of the introductory examples in
the ggplot documentation, which will give you a better grounding. As
to your question - yes, basically you want to (1) melt it, e.g. dmelt
<- melt( d, id.vars=c("id","x") ) and (2) plot it, qplot( x, value,
data=dmelt, geom="line", color=id ).

Ben


On Thu, Nov 8, 2012 at 10:54 AM, Jorge Ahumada
<jahu...@conservation.org> wrote:
> Ok, but how do you add a legend to this for denoting what the color of each
> line is?
>
> Jorge
>
> On Wednesday, November 7, 2012 12:02:25 AM UTC-5, Phil wrote:
>>
>> Personally, I think a more elegant solution (which also drops the need to
>> create extra variables) is to build the plot by using geom_lines, i.e.:
>>
>> # Create a data matrix
>> V = cbind(1:30,rnorm(1:30),rnorm(1:30)+3,rnorm(1:30)+6)
>> colnames(V) = c('time','var1','var2','var3')
>>
>> # Plot each of these columns
>> d = ggplot(as.data.frame(V), aes(time)) +
>> geom_line(aes(y=var1),colour='red') +
>> geom_line(aes(y=var2),colour='blue') +
>> geom_line(aes(y=var3),colour='black')
>>
>> d + coord_cartesian(ylim=c(-2,8))
>>
>>
>>
>> As usual, there are several ways to skin a cat...
>>
>> Phil.

Jorge Ahumada

unread,
Nov 8, 2012, 12:29:20 PM11/8/12
to Ben Bond-Lamberty, ggp...@googlegroups.com
I know how to do a graph with a legend by using the appropriate aesthetics argument for grouping the data, but in your example you dont group the data in any way.. You add each layer sequentially to your graph. The question is: Is there still a way to add a legend MANUALLY in this situation?

Jorge

Dennis Murphy

unread,
Nov 8, 2012, 5:49:16 PM11/8/12
to Jorge Ahumada, ggp...@googlegroups.com
Hi:

Here's what I think Ben meant in terms of the toy example (rewritten with cleaner code):

V <- data.frame( time = seq(30), var1 = rnorm(30),
                  var2 = rnorm(30) + 3, var3 = rnorm(30) + 6)
library('reshape2')

# Reshape V from 'wide' form to 'long' form
Vm <- melt(V, id = "time")
str(Vm)
levels(Vm$variable)
head(Vm)

# This form is convenient for ggplot() because now the
# variables Var1-Var3 can be accessed as a single entity:

ggplot(Vm, aes(x = time, y = value, color = variable)) +
    geom_line() +
    coord_cartesian(ylim = c(-2, 8)) +
    scale_color_manual(values = c("red", "blue", "black"))

You can create a factor on the fly, but it is not immediately obvious how to do so; fortunately, Hadley provided an example in the ggplot2 book. The idea is to put the aesthetic to be created in aes() and sequentially augment its set of values with multiple calls to the geom. You can then use the appropriate scale_* function to fine tune the legend guide.

ggplot(V, aes(x = time)) +
   geom_line(aes(y = var1, color = "var1")) +
   geom_line(aes(y = var2, color = "var2")) +
   geom_line(aes(y = var3, color = "var3")) +
   coord_cartesian(ylim = c(-2, 8)) +
   labs(color = "Variable") +
   scale_color_manual(values = c("red", "blue", "black"))

Is that what you were asking about?

Dennis

Jorge Ahumada

unread,
Nov 8, 2012, 6:16:12 PM11/8/12
to Dennis Murphy, ggp...@googlegroups.com
Yes, exactly!
Thanks much..

J.
Reply all
Reply to author
Forward
0 new messages