0

Here is the data I am using and the code:

 str(dfrev_benchmark2)
'data.frame':   20 obs. of  2 variables:
 $ B_Vec  : num  1 51 101 151 201 251 301 351 401 451 ...
 $ revenue: num  31508 1606929 3182350 4757771 6333192 ...

str(dfaugrev2)
'data.frame':   20 obs. of  2 variables:
 $ B_Vec  : num  1 51 101 151 201 251 301 351 401 451 ...
 $ revenue: num  45451 2317977 4590503 6863029 9135556 ...

str(dfjanrev2)
'data.frame':   20 obs. of  2 variables:
 $ B_Vec  : num  1 51 101 151 201 251 301 351 401 451 ...
 $ revenue: num  18412 939015 1859618 2780220 3700823 ...

profitmonthly <- ggplot() + 
geom_line(data=dfjanrev2, aes(x=dfjanrev2[,1], y=dfjanrev2[,2]), 
        color="#000666", size=1.3)+ 
geom_line(data=dfaugrev2, aes(x=dfaugrev2[,1], y=dfaugrev2[,2]), 
        color="#990033", size=1.3)+
geom_line(data=dfrev_benchmark2, aes(x=dfrev_benchmark2[,1],  y=dfrev_benchmark2[,2]), 
        color="#006633", size=1.3)+
scale_y_continuous(labels = scales::dollar)+
scale_x_continuous(limits = c(0, 1000))+
xlab("Size of the battery")+
ylab("Profit") + 
ggtitle("Size of the Battery vs Profit: Monthly Forecast Timespan") +
theme(plot.title=element_text(size = 15, face="bold"), axis.text=element_text(size=12),
    axis.title=element_text(size=12,face="bold"))+
scale_color_manual(name = "Line Color",
                  labels = c("January"="#000666", "August"="#990033", "Yearly forecast"="#006633"))


print(profitmonthly)

It gives me this plot:

enter image description here

I have tried to put the color inside the aes, but then the plot is empty and it does not read the data. So I guess scale_color_manual is not useful here, since data is not factors.

I just need to create a simple legend describing the colors of the lines.

989
  • 12,579
  • 5
  • 31
  • 53
DariaOs
  • 1
  • 1

1 Answers1

0

You need to have it all in one single data frame and pass the color argument inside the aes.

dfjanrev2$month <- "January"
dfaugrev2$month <- "August"
dfrev_benchmark2$month <- "Yearly forecast"
data <- rbind(dfjanrev2, dfaugrev2, dfrev_benchmark2)

profitmonthly <- ggplot(data) + 
  geom_line(aes(x=B_Vec, y=revenue, color = month), size=1.3)+ 
  scale_y_continuous(labels = scales::dollar)+
  scale_x_continuous(limits = c(0, 1000))+
  xlab("Size of the battery")+
  ylab("Profit") + 
  ggtitle("Size of the Battery vs Profit: Monthly Forecast Timespan") +
  theme(plot.title=element_text(size = 15, face="bold"), axis.text=element_text(size=12),
    axis.title=element_text(size=12,face="bold"))+
  scale_color_manual(name = "Line Color",
                  values = c("January"="#000666", 
                             "August"="#990033",
                             "Yearly forecast"="#006633"))

edit: changed labels into values.

Choubi
  • 640
  • 3
  • 9
  • That worked, also changed 'labels' into 'values' cause otherwise gives an error. Thanks a lot!!! – DariaOs Jul 06 '16 at 09:40
  • Right, I edited the answer. To wit, there are other ways to force a legend with several `geom_lines` but it is not usually recommended, check out the second answer here: http://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot – Choubi Jul 06 '16 at 09:43