如何更改ggplot中的线条颜色

时间:2016-06-17 05:34:01

标签: r ggplot2

我有一个名为mymat的数据矩阵。下面是我用来绘制样本图(code)的key.related.sheet。我现在需要的是将落在IBS范围0.8-1.5内的线的颜色改为蓝色,0.4-0.6为红色,0.1-(-0.1)为黄色,任何低于-0.1的绿色。我们应该怎么做?

      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)

数据

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")))

1 个答案:

答案 0 :(得分:1)

如果你想改变线条各部分的颜色,那么你可以添加如下内容:

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"))

然后这样画:

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)

要使整条线条颜色相同,取决​​于您采用的颜色以及相应的规则。

但你可能会为此分组和汇总数据。