Search code examples
rggplot2graphcolorsscale-color-manual

How to draw different colors on graph line and insert legend (color and column title)


I've tried assigning colors to each line. But, can't figure out how to show legend result as I want.

groupcolours <- c(NE="#999999",NS="#E69F00",NW="#56B4E9",NN="#009E73")

I input the following code.

s <- ggplot(time_interval_RSE2) + 
  ggtitle(paste(main_rse_temp,"_",naljja,"_교통량", sep="")) + 
  labs(x="시간(15분 단위)", y="교통량(대수/15분)") +
  theme(axis.text.x=element_text(size=12,angle=90,hjust=1,vjust=0.5),axis.text.y = element_text(size=19),plot.title = element_text(size=25),axis.title.y=element_text(colour="black",size=30),axis.title.x = element_text(colour="black",size=30)) + 
  scale_y_continuous(limits=c(0, 100)) + 
  geom_line(aes(x=time_interval,y=as.numeric(NE),group=1,colour=groupcolours)) +
  geom_line(aes(x=time_interval,y=as.numeric(NS),group=2,colour=groupcolours)) +
  geom_line(aes(x=time_interval,y=as.numeric(NW),group=3,colour=groupcolours)) +
  geom_line(aes(x=time_interval,y=as.numeric(NN),group=4,colour=groupcolours)) + 
  scale_color_manual(name="Direction", values =groupcolours)

I get this error.

Error: Aesthetics must be either length 1 or the same as the data (96): colour

"time_interval_RSE2", the table to draw the graph "time_interval_RSE2", the table to draw the graph

The graph result I want The graph result I want

*I have uploaded the image file. "dput(time_interval_RSE2)" doesn't give exact same values(for example, '0' is shown as '0L').

["time_interval_RSE2" file download][3]

Solution

  • as mentioned in the comments, please don't add the data as an image. Rather use dput, so everybody can work with the data.

    Looking at your data image, it seems that the data is in wide format. To draw the attached graph you could convert the data into long format (using the package reshape2 or the gather function from dplyr) and then add the Direction variable as group and color variable.

    For example (with simulated data):

    library(reshape2)
    library(tidyverse)
    
    Timestamp <- c("30-07-2019 23:00:00", "31-07-2019 00:00:00", "31-07-2019 01:00:00", 
    "31-07-2019 02:00:00", "31-07-2019 03:00:00", "31-07-2019 04:00:00", 
    "31-07-2019 05:00:00", "31-07-2019 06:00:00", "31-07-2019 07:00:00", 
    "31-07-2019 02:00:00", "31-07-2019 09:00:00", "31-07-2019 10:00:00", 
    "31-07-2019 11:00:00", "31-07-2019 12:00:00", "31-07-2019 13:00:00", 
    "31-07-2019 14:00:00", "31-07-2019 15:00:00", "31-07-2019 16:00:00", 
    "31-07-2019 17:00:00", "31-07-2019 18:00:00", "31-07-2019 19:00:00", 
    "31-07-2019 20:00:00", "31-07-2019 21:00:00", "31-07-2019 22:00:00", 
    "31-07-2019 23:00:00", "01-08-2019 00:00:00", "01-08-2019 01:00:00"
    )
    
    dat <- data.frame(Timestamp, 
                      NE = sample(1:10, length(Timestamp), replace=TRUE), 
                      NS = sample(1:16, length(Timestamp), replace=TRUE), 
                      NW = sample(1:3, length(Timestamp), replace=TRUE))
    
    melt(dat, id="Timestamp") %>% 
         ggplot(aes(Timestamp, value, group=variable, color=variable)) + 
         geom_line()
    

    The resulting image looks like this:

    resulting image

    Please keep in mind that you have to change the variable names according to your data as this is only simulated data.