0

I want to plot several different data sets with the same set of legends but not all data sets have all the legend labels so I want to just plot one legend for all and use the same colors for each legend label.

My data looks something like this

Sample  Activity    Location    Value
brain   A1  -99 0.000480219165072995
brain   A1  -98 0.000310998665750027
brain   A1  -97 0.00013269798404962
brain   A1  -96 0.000414032362112828
brain   A1  -95 0.000484106264682014
brain   A1  -94 0.000277469810522874
brain   A1  -93 -0.000312328089983588
brain   A1  -92 -0.000326948367221977
brain   A1  -91 -0.000566097491837788
brain   A2  -99 0.023199362386866
brain   A2  -98 0.0232008290610013
brain   A2  -97 0.0235067519290527
brain   A2  -96 0.0235475873183088
brain   A2  -95 0.0237440466425034
brain   A2  -94 0.0240249966894288
brain   A2  -93 0.0245502842927103
brain   A2  -92 0.0244587160446747
brain   A2  -91 0.0252699000904297

So I want to plot two lines for Activity, one color for A1 and another color for A2 and etc.

There are about 8 different activities and many locations.

How do I manually set up colors for each Activity? For example, A1 will always be in red, A2 in black, A3 in blue etc..?

ggplot(data=df,aes(x=Location,y=Value,group=Activity))+geom_line(aes(colour=Activity),size=1.5)+theme_bw()
olala
  • 4,146
  • 9
  • 34
  • 44
  • 1
    You want to manually set up colors for your scales? How about [scale_color_manual](http://docs.ggplot2.org/current/scale_manual.html). – MrFlick Jul 31 '14 at 15:28
  • yeah, seems like this works: cols <- c("8" = "red","4" = "blue","6" = "darkgreen", "10" = "orange") p + scale_colour_manual(values = cols) – olala Jul 31 '14 at 15:36
  • possible duplicate of [R: changing line colors with ggplot()](http://stackoverflow.com/questions/5171263/r-changing-line-colors-with-ggplot) – shadow Jul 31 '14 at 16:33

1 Answers1

1

Simply following the advise in @MrFlick's comment (code below)

scale_color_manual

df <- structure(list(Sample = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "brain", class = "factor"), 
    Activity = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A1", "A2"
    ), class = "factor"), Location = c(-99L, -98L, -97L, -96L, 
    -95L, -94L, -93L, -92L, -91L, -99L, -98L, -97L, -96L, -95L, 
    -94L, -93L, -92L, -91L), Value = c(0.000480219165072995, 
    0.000310998665750027, 0.00013269798404962, 0.000414032362112828, 
    0.000484106264682014, 0.000277469810522874, -0.000312328089983588, 
    -0.000326948367221977, -0.000566097491837788, 0.023199362386866, 
    0.0232008290610013, 0.0235067519290527, 0.0235475873183088, 
    0.0237440466425034, 0.0240249966894288, 0.0245502842927103, 
    0.0244587160446747, 0.0252699000904297)), .Names = c("Sample", 
"Activity", "Location", "Value"), class = "data.frame", row.names = c(NA, 
-18L))


# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)

p <- ggplot(data=df,aes(x=Location,y=Value,group=Activity))+geom_line(aes(colour=Activity),size=1.5)+theme_bw()
p + scale_color_manual(values=c("red", "blue")) # add more … 
Eric Fail
  • 8,191
  • 8
  • 72
  • 128