How to change the colour of the lines in ggplot

MAPK

I have a data matrix called mymat. Below is the code I have used to plot the graph for the samples (key.related.sheet). What I need now is to change the colour of the lines that fall within the IBS range of 0.8-1.5 into blue, 0.4-0.6 in red and 0.1- (-0.1) in yellow and anything below -0.1 in green. How can we do this?

code

      library(data.table)
      library(ggplot2)
      library(dplyr)
      cpp <- melt(as.data.table(mymat), measure =  patterns("^IBS", "^Num_Good"), 
         value.name = c("IBS", "Num_Good")) %>%
      .[, c("IBS", "Num_Good") := lapply(.SD, as.numeric), .SDcols = IBS:Num_Good]
      ggplot(cpp, aes(x = Num_Good, y = IBS, col = key.related.sheet)) + 
      geom_line() + 
      #geom_vline(xintercept = 0)+
      geom_hline(yintercept = 0)+
      geom_hline(yintercept = 0.5)+
      geom_hline(yintercept = 1.0)+
      scale_colour_discrete(guide = FALSE)

data

mymat<- structure(c("AOGC-02-0010:AOGC-02-0010", "AOGC-02-0010:AOGC-02-0022", 
"AOGC-02-0010:AOGC-02-0063", "AOGC-02-0010:AOGC-02-0079", "AOGC-02-0010:AOGC-02-0087", 
"AOGC-02-0010:AOGC-02-0105", "AOGC-02-0010:AOGC-02-0108", "AOGC-02-0010:AOGC-02-0112", 
"AOGC-02-0010:AOGC-02-0118", "AOGC-02-0010:AOGC-02-0161", "AOGC-02-0010:AOGC-02-0509", 
"AOGC-02-0010:AOGC-02-0512", "AOGC-02-0010:AOGC-02-0513", "AOGC-02-0010:AOGC-02-0522", 
"AOGC-02-0010:AOGC-02-0523", "AOGC-02-0010:AOGC-03-0094", "AOGC-02-0010:AOGC-03-0114", 
"AOGC-02-0010:AOGC-03-0122", "AOGC-02-0010:AOGC-03-0132", "AOGC-02-0010:AOGC-03-0138", 
"AOGC-02-0010:AOGC-05-0009", "1.12", "-0.00346", "0.0132", "0.0238", 
"0.00404", "-0.0201", "-0.00653", "-0.00466", "-0.00942", "-0.00484", 
"-0.0085", "-0.00231", "-0.03", "-0.0235", "-0.00412", "-0.0025", 
"-0.0111", "0.000753", "-0.0231", "-0.0205", "-0.0121", "67289", 
"52782", "51066", "46950", "47943", "51516", "59956", "57771", 
"52397", "58087", "54291", "33791", "24889", "65773", "65777", 
"51710", "55043", "50168", "64144", "64580", "57970", "1.13", 
"0.500283", "0.0108", "0.0227", "0.400507", "-0.0219", "-0.00914", 
"0.800371", "-0.0147", "-0.00372", "-0.00817", "-0.00983", "-0.0248", 
"-0.0205", "-0.012", "-0.0127", "-0.0107", "0.0106", "-0.026", 
"-0.023", "-0.900788", "53519", "42192", "40667", "37442", "38262", 
"40952", "47671", "45966", "41675", "46191", "43512", "27316", 
"20038", "52297", "52304", "41075", "43963", "39989", "51040", 
"51390", "46087", "1.12", "-0.00143", "0.00778", "0.0119", "0.00842", 
"-0.0237", "-0.0168", "0.00345", "-0.0112", "0.00414", "-0.00337", 
"-0.0097", "-0.0322", "-0.0275", "-0.0173", "-0.017", "-0.0081", 
"0.0256", "-0.0327", "-0.00254", "-0.0166", "26864", "21262", 
"20553", "18909", "19335", "20543", "23915", "23002", "20917", 
"23130", "22233", "14524", "10685", "26261", "26244", "20542", 
"22356", "20132", "25647", "25777", "23045", "1.15", "0.00715", 
"-0.00543", "0.0192", "0.0254", "-0.00775", "-0.00167", "0.00523", 
"-0.027", "0.0125", "0.00182", "-0.0262", "-0.0526", "-0.0178", 
"-0.0413", "-0.00793", "-0.00371", "0.0287", "-0.0141", "-0.00325", 
"-0.0199", "13923", "10935", "10434", "9755", "9929", "10669", 
"12505", "11958", "10818", "12048", "11064", "6635", "4757", 
"13599", "13612", "10740", "11252", "10394", "13261", "13371", 
"12042", "1.14", "-0.0122", "0.0103", "0.0189", "0.04", "0.0084", 
"0.0227", "-0.0101", "-0.0328", "-0.0134", "-0.00669", "-0.0421", 
"-0.0442", "-0.00531", "-0.044", "0.0526", "-0.00056", "0.0221", 
"0.0182", "0.0259", "-0.0362", "7240", "5750", "5535", "5151", 
"5202", "5567", "6525", "6231", "5618", "6291", "5892", "3645", 
"2674", "7084", "7084", "5567", "5962", "5481", "6925", "7004", 
"6279", "1.18", "0.0143", "-0.0763", "-0.00882", "0.0526", "-0.0195", 
"-0.0531", "-0.00064", "-0.00616", "-0.00746", "0.0173", "-0.0795", 
"0.0041", "0.018", "-0.0522", "0.0767", "0.0476", "0.000252", 
"0.0456", "0.0377", "-0.0391", "3527", "2766", "2691", "2487", 
"2461", "2672", "3135", "3010", "2680", "3056", "2846", "1707", 
"1265", "3444", "3444", "2682", "2886", "2638", "3366", "3402", 
"2982"), .Dim = c(21L, 13L), .Dimnames = list(c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "30", "31", "32", "33", "34", 
"35", "36", "37", "38", "39", "40"), c("key.related.sheet", "IBS_from:1305328SNPS", 
"Num_Good_SNPs_A:1305328SNPS", "IBS_from:1030725SNPS", "Num_Good_SNPs_A:1030725SNPS", 
"IBS_from:514953SNPS", "Num_Good_SNPs_A:514953SNPS", "IBS_from:257361SNPS", 
"Num_Good_SNPs_A:257361SNPS", "IBS_from:128603SNPS", "Num_Good_SNPs_A:128603SNPS", 
"IBS_from:64000SNPS", "Num_Good_SNPs_A:64000SNPS")))
zacdav

if you want to change the color of only sections of the line then you can add in something like this:

cpp$cutoff = cut(cpp$IBS, 
    breaks = c(-Inf, -0.1, 0.1, 0.4, 0.6, 0.8, 1.5),
    labels = c("GREEN", "YELLOW", "GREY", "RED", "BLACK", "BLUE"))

and then plot like this:

ggplot(cpp, aes(x = Num_Good, y = IBS, group = key.related.sheet, color = cutoff)) + 
    geom_line() + geom_point()+
    #geom_vline(xintercept = 0)+
    geom_hline(yintercept = 0)+
    geom_hline(yintercept = 0.5)+
    geom_hline(yintercept = 1.0)+
    scale_colour_discrete(guide = FALSE)

To make the entire lines the same color it depends on what color you which to adopt and the rules around that.

But you would probably group and summarise the data for that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change the colour of the lines representing range of variables in ggplot?

How to colour lines in ggplot depending on a condition?

How to make a boxplot in ggplot where median lines change colour depending on the group?

Colour multiple lines in ggplot

How to change the line colour for different lines in a subplot

Matplotlib / Mplfinance - how to change colour of lines and bars

How to change font size and colour directlabels in ggplot

How do I change the border lines COLOUR on the table view

How to change background colour of legend in ggplot2?

How do I change the background colour of a legend box in ggplot?

How to change fill colour of empty cells in ggplot heatmap

How to change the text in the colour legend of ggplot2

How to change colour for barplot in ggplot2 using viridis package?

change colour of legend box in ggplot

How to change the colour of nodes?

How to colour multiple geom_lines on a gradient using ggplot2 in R?

Change default colour lines xyplot with referencing

ggplot change fill colour without losing colour gradient

Change the colour of specific observations on boxplot using ggplot

Change shape, size, colour in ggplot objects

How to change legend title and key order and colour to a multi-stacked barplot in R with ggplot2

How to change legend shapes in R using ggplot and scale_colour_brewer()

How to change the colour for missing value in geom_point of ggplot2?

ggplot2: how to change the width of colour aesthetic in a bar/col geometry?

How to change colour and position of geom_text for just one bar in a barplot in ggplot2 (R)?

ggplot2: geom_bar fill colour; how to change to different grouping of data

How to change the colour of the statusbar in android?

How to change grayscale to colour in python?

How to change the background colour of SKScene?