0

My Dataframe looks like:

product=c("p1","p1","p1","p2","p2","p2","p3","p3","p3")
varX=c(1,2,3)
var1=c(1,1,2,3,4,5,3,2,1)
var2=c(10,11,10,8,8,8,5,6,8)
da <- data.frame(varX, product, var1, var2)

ggplot(da, aes(varX)) + geom_line(aes(y = var1, colour = product)) + geom_line(aes(y = var2, colour = product))

Doing this leads to a plot with 6 lines where 2 are always the same colour. What I actually need is 6 different colours and the legend like.

-- p1(var1)
-- p1(var2)
-- p2(var1)
-- p2(var2)
-- p3(var1)
-- p3(var2)

And also a regression for var1 and var2 (distinct) would be nice to have in the same plot.

Dennis Ich
  • 3,585
  • 6
  • 27
  • 44
  • 1
    `reshape2::melt` your data.frame to long format. – Roland Mar 02 '16 at 10:31
  • I don't think this is answered there. If I do that I can group by "product" or by "variable". I need both at the same time as the variables are else "mixed" by the product which is not desired. Each product has 3 Values for var1 and var2 (Actually I have several thousand Products with some hundred variables and loads of values).... So I need 1 line per Product AND Variable. – Dennis Ich Mar 02 '16 at 11:06
  • 1
    You can do e.g. `color = interaction(product, variable)`, or `color = paste(product, variable, sep = ".")` – Henrik Mar 02 '16 at 11:23
  • Thats what I needed thanks! – Dennis Ich Mar 02 '16 at 12:13

0 Answers0