rggplot2colorsmanual

geom line: set manual color for variables that are grouped


I've data set with three columns as below

data_sample<-data.frame(value=c(-3,-2,-1,-0.9,-1.25,1.0,2.1,1.9,1.2,1.5),
BP=c(1000,2000,5000,8000,10000),variable=c(rep("overall_diff_cases",5),rep("overall_diff_controls",5)))

where BP is base pair, for each position, there's value in variable column: overall_diff_controls and overall_diff_cases. I'm interested in creating a geom_line ggplot graph using these points.

ggplot(data_sample, aes(x=BP, y=value, group =variable,color=variable)) +
scale_fill_manual(values= c("overall_diff_cases"="#9633FF" ,"overall_diff_controls"="#E2FF33")) + geom_line(size=1.2)

If I use the above code, ggplot creates plot with default color. I tried with color code (non hexa decimal)

scale_fill_manual(values= c("overall_diff_cases" = "red" , "overall_diff_controls" = "black"))

I tried setting up setNames but in vain:

ggplot(data_sample, aes(x=BP, y=value, group = variable,color=variable)) +
scale_fill_manual(values=setNames(c("overall_diff_cases","overall_diff_controls"), c( "black","red"))) +
 geom_line(size=1.2)
 

How do I set the colors of my interests? The data format needs to stay as shown in the same data frame.


Solution

  • Try this:

    library(ggplot2)
    
    ggplot(data_sample, aes(x=BP, y=value,
                            group =variable,color=variable)) +
      scale_color_manual(values= c("overall_diff_cases"="#9633FF" ,
                                   "overall_diff_controls"="#E2FF33")) + geom_line(size=1.2)
    

    Output:

    enter image description here

    With scale_color_manual() you can set the colors you want.