-1
mydata = read.csv("Group+3+puppies.csv")
head(mydata)
X = mydata$Age, Y1=mydata$LP.10, Y2=mydata$LP.11, Y3=mydata$LP.12, Y4=mydata$LP.13, Y5=mydata$LP.14, Y6=mydata$LP.15, Y7=mydata$LP.16, Y8=mydata$LP.1, Y9=mydata$LP.18, Y10=mydata4Average
library(ggplot2)
ggplot(ggplot(mydata, aes(x=Age, y=Weight)) + geom_line(aes(y = Y1), color = "darkblue") + geom_line(aes(y = Y2), color="lightblue") + geom_line(aes(y = Y3), color="darkgreen") + geom_line(aes(y = Y4), color="brown") + geom_line(aes(y = Y5), color="gray") + geom_line(aes(y = Y6), color="black") + geom_line(aes(y = Y7), color="pink") + geom_line(aes(y = Y8), color="yellow") + geom_line(aes(y = Y9), color="red") + geom_line(aes(y = Y10), color="orange")

I get this graph: (the graph I want) graph of data

but I cannot figure out how to add a legend to this, legend would be LP10-LP18 and the average line (all different colors). I have tried the legends() function and I know there is a way to do this using melt(), but I am not sure how to go about this.

data is currently structured like this: image of data with age in days, each number below and LP number is a weight and average is a weight

mhonig8
  • 1
  • 1
  • check that you have the latest version – MEdwin Jun 25 '19 at 16:02
  • 1
    Welcome to SO! This turns out to be a frequently-asked ggplot question, I suggest searching for "[ggplot2] melt" etc. Here's a popular answer from 2010: https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph/3777592#3777592 – Jon Spring Jun 25 '19 at 16:04

1 Answers1

0

Consider using ggplot with tidy data instead of wide data matrix form.

library(tidyr)
library(ggplot2)
mydata <- gather(mydata, key = "Variable", value = "measurement", -Age, -Weight)
ggplot(mydata, aes(x = Age, y = Weight, color = Variable) + 
    color_scale_manual(values = c( "darkblue", "lightblue", "darkgreen",
                                   "brown", "gray","black", "pink",
                                   "yellow", "red", "orange"))

This might not entirely work because I have no idea how your data frame is structured or the difference between your LP10:LP18 vectors and the Weight vector.

Justin Landis
  • 1,981
  • 7
  • 9