0

I need to set five colors ranging from yellow - orange - red for 6 lines in a graph. I have tried the names and the hex numbers but they don't seem to work!

Example Data:

 Year EF0 EF1 EF2 EF3 EF4 EF5
 1950  13  83  71  26   7   0
 1951  25  88  82  22   5   0
 1952  24  80  72  38  20   0
 1953  66 160 143  46  22   5
 1954  91 226 193  49  11   0
 1955 169 217 167  32  12   6
 1956 125 184 149  38  21   1
 1957 218 305 238  74  25   7
 1958 145 233 154  39   4   1
 1959 143 264 157  37  11   0
 1960 128 262 175  47   6   1

Current GGPLOT2 Code

ggplot(dTorYear, aes(x=dTorYear$Year))+ 
  geom_line(aes(y = dTorYear$EF0, colour = "var5")) + 
  geom_line(aes(y = dTorYear$EF1, colour = "var4")) +
  geom_line(aes(y = dTorYear$EF2, colour = "var3")) +
  geom_line(aes(y = dTorYear$EF3, colour = "var2")) +
  geom_line(aes(y = dTorYear$EF4, colour = "var1")) +
  geom_line(aes(y = dTorYear$EF5, colour = "var0")) +
  xlab("Year") +
  ylab("Tornado Count")

Any help would be much appreciated!

Methexis
  • 2,739
  • 5
  • 24
  • 34
  • I don't see where you made an attempt to set the color values in your code. Did you leave that part out? Where did you specify the hex colors? – MrFlick Sep 23 '14 at 14:36
  • i put hex colors where the var0 var1 etc are and they came out in all sorts of odd colors that did not relate back to the hex code – Methexis Sep 23 '14 at 14:44
  • 1
    @Methexis, In `ggplot` it is generally easier to work with the data in a 'long' format. There are heaps and heaps of posts on this topic on SO, see e.g. [**here**](http://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph/3777592#3777592). Also check `scale_colour_manual` and other `scale_colour_xxx` functions. – Henrik Sep 23 '14 at 14:51
  • If you have your answer, please accept either arvi1000 or myself, seeing we have the same solution. Either one, that way the question is officially answered. – cdeterman Sep 23 '14 at 15:25

2 Answers2

1

The RColorBrewer package is nice for things like this. It will generate hex numbers for you along a defined gradient. The primary issue in your code, however, is the location of our colour statement as it is part of geom_line not aes.

Switching your data into long format makes everything much easier.

require(ggplot2)
require(reshape2)
require(RColorBrewer)

cols<-colorRampPalette(c('yellow','red'))(ncol(dTorYear)-1)

dat <- melt(dTorYear, id=c("Year"))

ggplot(dat)+
  geom_line(aes(x = Year, y = value, colour = variable)) +
  xlab("Year") +
  ylab("Tornado Count")+
  scale_colour_manual(values=c(cols))

enter image description here

cdeterman
  • 19,630
  • 7
  • 76
  • 100
0

If you melt the data frame, you can simplify the ggplot call and get your legend.

# your data
dTorYear <- read.table(text=' Year EF0 EF1 EF2 EF3 EF4 EF5
 1950  13  83  71  26   7   0
 1951  25  88  82  22   5   0
 1952  24  80  72  38  20   0
 1953  66 160 143  46  22   5
 1954  91 226 193  49  11   0
 1955 169 217 167  32  12   6
 1956 125 184 149  38  21   1
 1957 218 305 238  74  25   7
 1958 145 233 154  39   4   1
 1959 143 264 157  37  11   0
 1960 128 262 175  47   6   1', header=T)


library(reshape2)
library(ggplot2)

# color sequence, already posted by user 'charles'
cols <- colorRampPalette(colors=c('yellow', 'red'))(ncol(dTorYear)-1)

# molten data.frame
plot_df <- melt(dTorYear, id.var='Year')

# plot!
ggplot(plot_df, aes(x=Year, y=value, color=variable)) + 
  geom_line() + 
  scale_color_manual(values=cols)

enter image description here

arvi1000
  • 9,393
  • 2
  • 42
  • 52