1
votes

I want to produce a line plot in R that contains multiple lines in different colours depending on other data in the table. My data looks similar to this:

mydf = data.frame(iteration=c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5), 
            value=runif(20, min=0, max=1),
            category=c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B"),
            subcategory=c("x","x","x","x","x","y","y","y","y","y","x","x","x","x","x","y","y","y","y","y"))


 iteration  value   category    subcategory
1   1   0.79813537  A   x
2   2   0.45196396  A   x
3   3   0.28001580  A   x
4   4   0.65997486  A   x
5   5   0.82217320  A   x
6   1   0.33805127  A   y
7   2   0.75842241  A   y
8   3   0.18502805  A   y
9   4   0.75586271  A   y
10  5   0.28269372  A   y
11  1   0.27585682  B   x
12  2   0.45901786  B   x
13  3   0.18962731  B   x
14  4   0.63682207  B   x
15  5   0.89821930  B   x
16  1   0.93757079  B   y
17  2   0.27272290  B   y
18  3   0.20485397  B   y
19  4   0.33647649  B   y
20  5   0.07788958  B   y

Now I would like to print four lines with ggplot into the same plot representing all available combinations of category and subcategory. I also want to have a different colour for each combination. For example: A.x red, A.y green, B.x blue, B.y yellow.

The best I could come up with was two colours and two shapes to distinguish between lines.

ggplot(data=mydf, 
   aes(x=iteration, y=value, colour=category, shape=subcategory)) + 
 geom_line() + geom_point()

Is there a way of assigning a colour for each possible permutation of category and subcategory?

Thanks!

(I would like to show some sample images but I don't have enough rep yet.)

1
Not the solution but another way of distinguishing would be using linetype instead of shape for the subcategory. - vseeker

1 Answers

1
votes

Create a new variable holding both category and subcategory and map it to colour.

mydf$group <- paste(mydf$category,mydf$subcategory,sep="_")

ggplot(data=mydf, 
   aes(x=iteration, y=value, colour=group)) + 
 geom_line() + geom_point()  +
 scale_color_manual(values = c("red","green","blue","yellow"))

enter image description here