不同的线型和GGPLOT曲线中的固定颜色

时间:2014-07-01 01:45:33

标签: r plot ggplot2

下图 enter image description here

使用以下代码

创建

#!/usr/bin/env Rscript
library(ggplot2)
library(RColorBrewer)


dat <- structure(list(TPR = c(0, 0.0850833220431, 0.108565604562, 0.121399539358, 
    0.127847298346, 0.152790949736, 0.163810233482, 0.180974423821, 
    0.193273269205, 0.216413765072, 0.231269475681, 0.241180056903, 
    0.256868987942, 0.267606015445, 0.28826717247, 0.306448990652, 
    0.339432326243, 0.36087928465, 0.379040780382, 0.383173011787, 
    0.392257146728, 0.407119631486, 0.421142121664, 0.425274353069, 
    0.429406584474, 0.443449397101, 0.472375016935, 0.502912884433, 
    0.507045115838, 0.531811407668, 0.558968974394, 0.578783362688, 
    0.615099580003, 0.640705866414, 0.701856117057, 0.709753270409, 
    0.732427855304, 0.741511990245, 0.74885140526, 0.759666711828, 
    0.762146050671, 0.772889852324, 0.77867497629, 0.783626879827, 
    0.792717788917, 0.798496138735, 0.805107708983, 0.810892832949, 
    0.81171927923, 0.815025064354, 0.824935645576, 0.8282414307, 
    0.833179785937, 0.837305243192, 0.843083593009, 0.84391003929, 
    0.845556157702, 0.850514835388, 0.854647066793, 0.863737975884, 
    0.867023438558, 0.869502777401, 0.873635008806, 0.877760466062, 
    0.881045928736, 0.881045928736, 0.888483945265, 0.888483945265, 
    0.889310391546, 0.890963284108, 0.895095515513, 0.900054193199, 
    0.906665763447, 0.911624441133, 0.914103779976, 0.916583118819, 
    0.922368242786, 0.92566725376, 0.927320146322, 0.929799485165, 
    0.93393171657, 0.936397507113, 0.937223953394, 0.941356184799, 
    0.945481642054, 0.947127760466, 0.95043354559, 0.957045115838, 
    0.961170573093, 0.964476358217, 0.966129250779, 0.967782143341, 
    0.970261482184, 0.973567267308, 0.976873052432, 0.980172063406, 
    0.98347784853, 0.984304294811, 0.985950413223, 1, 0, 0.560227643367, 
    0.609755551268, 0.688443505643, 0.709951978804, 0.737954672042, 
    0.751951570987, 0.775028590562, 0.781286308417, 0.795511201981, 
    0.798605505901, 0.806485119451, 0.812833729518, 0.819175382968, 
    0.822922217706, 0.828964950959, 0.834219339673, 0.841317962172, 
    0.848260737028, 0.852028475835, 0.854876965929, 0.857841181083, 
    0.858731628255, 0.862635682572, 0.864363372631, 0.868978526955, 
    0.872823583608, 0.878744975839, 0.88220728488, 0.88665193786, 
    0.890487077014, 0.892547669455, 0.895051654603, 0.900895810447, 
    0.906124156481, 0.90858161179, 0.910208744988, 0.912822873284, 
    0.913607341341, 0.915136331476, 0.916292126887, 0.918213150335, 
    0.921448921173, 0.922729507618, 0.923820944745, 0.926550754489, 
    0.928014574844, 0.929681833525, 0.931176497965, 0.932658749333, 
    0.934050124943, 0.93575657271, 0.937489097436, 0.939197485384, 
    0.940917747547, 0.942633484347, 0.944412216003, 0.945977744209, 
    0.9474507365, 0.948949187136, 0.950413202511, 0.952483931028, 
    0.955048531795, 0.957390123247, 0.959469784766, 0.961303484794, 
    0.964277948165, 0.96577709163, 0.967395348305, 0.968886527936, 
    0.970277384502, 0.972535243492, 0.973713425846, 0.975000001592, 
    0.976285638194, 0.977531920212, 0.978615382281, 0.979731254008, 
    0.980825087167, 0.981996915205, 0.983228525835, 0.984417945513, 
    0.985548008948, 0.986393256047, 0.987226648958, 0.988085092747, 
    0.98971280555, 0.9905041282, 0.99129545085, 0.9920867735, 0.99287809615, 
    0.9936694188, 0.99446074145, 0.9952520641, 0.99604338675, 0.9968347094, 
    0.99762603205, 0.9984173547, 0.99920867735, 1, 0, 0.141841346788, 
    0.283682693575, 0.378949201061, 0.450322228289, 0.492399543577, 
    0.515244591286, 0.530820497802, 0.539498037777, 0.544477015104, 
    0.575940454995, 0.602263569626, 0.657659305424, 0.685193966491, 
    0.72448930437, 0.745987068901, 0.756393588598, 0.793748212377, 
    0.799831262086, 0.803964884814, 0.808756147484, 0.810806205942, 
    0.813502135112, 0.821747207366, 0.828550224407, 0.836598606174, 
    0.840042350981, 0.844458191979, 0.849084751928, 0.853059894119, 
    0.85578819815, 0.858303511108, 0.862367896095, 0.867741861173, 
    0.873877690691, 0.87988077062, 0.884176993573, 0.886875141326, 
    0.888996677063, 0.891148619213, 0.893172106645, 0.894911740716, 
    0.896793377296, 0.8986040198, 0.900414662303, 0.902225304807, 
    0.90403594731, 0.905846589814, 0.907657232318, 0.909467874821, 
    0.911278517325, 0.913089159828, 0.914899802332, 0.916710444835, 
    0.918521087339, 0.920331729843, 0.922142372346, 0.92395301485, 
    0.925763657353, 0.927574299857, 0.92938494236, 0.931195584864, 
    0.933006227368, 0.934816869871, 0.936627512375, 0.938438154878, 
    0.940248797382, 0.942059439886, 0.943870082389, 0.945680724893, 
    0.947491367396, 0.9493020099, 0.951112652403, 0.952923294907, 
    0.954733937411, 0.956544579914, 0.958355222418, 0.960165864921, 
    0.961976507425, 0.963787149928, 0.965597792432, 0.967408434936, 
    0.969219077439, 0.971029719943, 0.972840362446, 0.97465100495, 
    0.976461647453, 0.978272289957, 0.980082932461, 0.981893574964, 
    0.983704217468, 0.985514859971, 0.987325502475, 0.989136144979, 
    0.990946787482, 0.992757429986, 0.994568072489, 0.996378714993, 
    0.998189357496, 1, 0, 0.010101010101, 0.020202020202, 0.030303030303, 
    0.040404040404, 0.0505050505051, 0.0606060606061, 0.0707070707071, 
    0.0808080808081, 0.0909090909091, 0.10101010101, 0.111111111111, 
    0.121212121212, 0.131313131313, 0.141414141414, 0.151515151515, 
    0.161616161616, 0.171717171717, 0.181818181818, 0.191919191919, 
    0.20202020202, 0.212121212121, 0.222222222222, 0.232323232323, 
    0.242424242424, 0.252525252525, 0.262626262626, 0.272727272727, 
    0.282828282828, 0.292929292929, 0.30303030303, 0.313131313131, 
    0.323232323232, 0.333333333333, 0.343434343434, 0.353535353535, 
    0.363636363636, 0.373737373737, 0.383838383838, 0.393939393939, 
    0.40404040404, 0.414141414141, 0.424242424242, 0.434343434343, 
    0.444444444444, 0.454545454545, 0.464646464646, 0.474747474747, 
    0.484848484848, 0.494949494949, 0.505050505051, 0.515151515152, 
    0.525252525253, 0.535353535354, 0.545454545455, 0.555555555556, 
    0.565656565657, 0.575757575758, 0.585858585859, 0.59595959596, 
    0.606060606061, 0.616161616162, 0.626262626263, 0.636363636364, 
    0.646464646465, 0.656565656566, 0.666666666667, 0.676767676768, 
    0.686868686869, 0.69696969697, 0.707070707071, 0.717171717172, 
    0.727272727273, 0.737373737374, 0.747474747475, 0.757575757576, 
    0.767676767677, 0.777777777778, 0.787878787879, 0.79797979798, 
    0.808080808081, 0.818181818182, 0.828282828283, 0.838383838384, 
    0.848484848485, 0.858585858586, 0.868686868687, 0.878787878788, 
    0.888888888889, 0.89898989899, 0.909090909091, 0.919191919192, 
    0.929292929293, 0.939393939394, 0.949494949495, 0.959595959596, 
    0.969696969697, 0.979797979798, 0.989898989899, 1), FPR = c(0, 
    0.010101010101, 0.020202020202, 0.030303030303, 0.040404040404, 
    0.0505050505051, 0.0606060606061, 0.0707070707071, 0.0808080808081, 
    0.0909090909091, 0.10101010101, 0.111111111111, 0.121212121212, 
    0.131313131313, 0.141414141414, 0.151515151515, 0.161616161616, 
    0.171717171717, 0.181818181818, 0.191919191919, 0.20202020202, 
    0.212121212121, 0.222222222222, 0.232323232323, 0.242424242424, 
    0.252525252525, 0.262626262626, 0.272727272727, 0.282828282828, 
    0.292929292929, 0.30303030303, 0.313131313131, 0.323232323232, 
    0.333333333333, 0.343434343434, 0.353535353535, 0.363636363636, 
    0.373737373737, 0.383838383838, 0.393939393939, 0.40404040404, 
    0.414141414141, 0.424242424242, 0.434343434343, 0.444444444444, 
    0.454545454545, 0.464646464646, 0.474747474747, 0.484848484848, 
    0.494949494949, 0.505050505051, 0.515151515152, 0.525252525253, 
    0.535353535354, 0.545454545455, 0.555555555556, 0.565656565657, 
    0.575757575758, 0.585858585859, 0.59595959596, 0.606060606061, 
    0.616161616162, 0.626262626263, 0.636363636364, 0.646464646465, 
    0.656565656566, 0.666666666667, 0.676767676768, 0.686868686869, 
    0.69696969697, 0.707070707071, 0.717171717172, 0.727272727273, 
    0.737373737374, 0.747474747475, 0.757575757576, 0.767676767677, 
    0.777777777778, 0.787878787879, 0.79797979798, 0.808080808081, 
    0.818181818182, 0.828282828283, 0.838383838384, 0.848484848485, 
    0.858585858586, 0.868686868687, 0.878787878788, 0.888888888889, 
    0.89898989899, 0.909090909091, 0.919191919192, 0.929292929293, 
    0.939393939394, 0.949494949495, 0.959595959596, 0.969696969697, 
    0.979797979798, 0.989898989899, 1, 0, 0.010101010101, 0.020202020202, 
    0.030303030303, 0.040404040404, 0.0505050505051, 0.0606060606061, 
    0.0707070707071, 0.0808080808081, 0.0909090909091, 0.10101010101, 
    0.111111111111, 0.121212121212, 0.131313131313, 0.141414141414, 
    0.151515151515, 0.161616161616, 0.171717171717, 0.181818181818, 
    0.191919191919, 0.20202020202, 0.212121212121, 0.222222222222, 
    0.232323232323, 0.242424242424, 0.252525252525, 0.262626262626, 
    0.272727272727, 0.282828282828, 0.292929292929, 0.30303030303, 
    0.313131313131, 0.323232323232, 0.333333333333, 0.343434343434, 
    0.353535353535, 0.363636363636, 0.373737373737, 0.383838383838, 
    0.393939393939, 0.40404040404, 0.414141414141, 0.424242424242, 
    0.434343434343, 0.444444444444, 0.454545454545, 0.464646464646, 
    0.474747474747, 0.484848484848, 0.494949494949, 0.505050505051, 
    0.515151515152, 0.525252525253, 0.535353535354, 0.545454545455, 
    0.555555555556, 0.565656565657, 0.575757575758, 0.585858585859, 
    0.59595959596, 0.606060606061, 0.616161616162, 0.626262626263, 
    0.636363636364, 0.646464646465, 0.656565656566, 0.666666666667, 
    0.676767676768, 0.686868686869, 0.69696969697, 0.707070707071, 
    0.717171717172, 0.727272727273, 0.737373737374, 0.747474747475, 
    0.757575757576, 0.767676767677, 0.777777777778, 0.787878787879, 
    0.79797979798, 0.808080808081, 0.818181818182, 0.828282828283, 
    0.838383838384, 0.848484848485, 0.858585858586, 0.868686868687, 
    0.878787878788, 0.888888888889, 0.89898989899, 0.909090909091, 
    0.919191919192, 0.929292929293, 0.939393939394, 0.949494949495, 
    0.959595959596, 0.969696969697, 0.979797979798, 0.989898989899, 
    1, 0, 0.010101010101, 0.020202020202, 0.030303030303, 0.040404040404, 
    0.0505050505051, 0.0606060606061, 0.0707070707071, 0.0808080808081, 
    0.0909090909091, 0.10101010101, 0.111111111111, 0.121212121212, 
    0.131313131313, 0.141414141414, 0.151515151515, 0.161616161616, 
    0.171717171717, 0.181818181818, 0.191919191919, 0.20202020202, 
    0.212121212121, 0.222222222222, 0.232323232323, 0.242424242424, 
    0.252525252525, 0.262626262626, 0.272727272727, 0.282828282828, 
    0.292929292929, 0.30303030303, 0.313131313131, 0.323232323232, 
    0.333333333333, 0.343434343434, 0.353535353535, 0.363636363636, 
    0.373737373737, 0.383838383838, 0.393939393939, 0.40404040404, 
    0.414141414141, 0.424242424242, 0.434343434343, 0.444444444444, 
    0.454545454545, 0.464646464646, 0.474747474747, 0.484848484848, 
    0.494949494949, 0.505050505051, 0.515151515152, 0.525252525253, 
    0.535353535354, 0.545454545455, 0.555555555556, 0.565656565657, 
    0.575757575758, 0.585858585859, 0.59595959596, 0.606060606061, 
    0.616161616162, 0.626262626263, 0.636363636364, 0.646464646465, 
    0.656565656566, 0.666666666667, 0.676767676768, 0.686868686869, 
    0.69696969697, 0.707070707071, 0.717171717172, 0.727272727273, 
    0.737373737374, 0.747474747475, 0.757575757576, 0.767676767677, 
    0.777777777778, 0.787878787879, 0.79797979798, 0.808080808081, 
    0.818181818182, 0.828282828283, 0.838383838384, 0.848484848485, 
    0.858585858586, 0.868686868687, 0.878787878788, 0.888888888889, 
    0.89898989899, 0.909090909091, 0.919191919192, 0.929292929293, 
    0.939393939394, 0.949494949495, 0.959595959596, 0.969696969697, 
    0.979797979798, 0.989898989899, 1, 0, 0.010101010101, 0.020202020202, 
    0.030303030303, 0.040404040404, 0.0505050505051, 0.0606060606061, 
    0.0707070707071, 0.0808080808081, 0.0909090909091, 0.10101010101, 
    0.111111111111, 0.121212121212, 0.131313131313, 0.141414141414, 
    0.151515151515, 0.161616161616, 0.171717171717, 0.181818181818, 
    0.191919191919, 0.20202020202, 0.212121212121, 0.222222222222, 
    0.232323232323, 0.242424242424, 0.252525252525, 0.262626262626, 
    0.272727272727, 0.282828282828, 0.292929292929, 0.30303030303, 
    0.313131313131, 0.323232323232, 0.333333333333, 0.343434343434, 
    0.353535353535, 0.363636363636, 0.373737373737, 0.383838383838, 
    0.393939393939, 0.40404040404, 0.414141414141, 0.424242424242, 
    0.434343434343, 0.444444444444, 0.454545454545, 0.464646464646, 
    0.474747474747, 0.484848484848, 0.494949494949, 0.505050505051, 
    0.515151515152, 0.525252525253, 0.535353535354, 0.545454545455, 
    0.555555555556, 0.565656565657, 0.575757575758, 0.585858585859, 
    0.59595959596, 0.606060606061, 0.616161616162, 0.626262626263, 
    0.636363636364, 0.646464646465, 0.656565656566, 0.666666666667, 
    0.676767676768, 0.686868686869, 0.69696969697, 0.707070707071, 
    0.717171717172, 0.727272727273, 0.737373737374, 0.747474747475, 
    0.757575757576, 0.767676767677, 0.777777777778, 0.787878787879, 
    0.79797979798, 0.808080808081, 0.818181818182, 0.828282828283, 
    0.838383838384, 0.848484848485, 0.858585858586, 0.868686868687, 
    0.878787878788, 0.888888888889, 0.89898989899, 0.909090909091, 
    0.919191919192, 0.929292929293, 0.939393939394, 0.949494949495, 
    0.959595959596, 0.969696969697, 0.979797979798, 0.989898989899, 
    1), Classifier = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 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2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("AB-DT Mean/AUC = 0.852", 
    "Luck Mean/AUC = 0.500", "RF Mean/AUC = 0.907", "SVM Mean/AUC = 0.695"
    ), class = "factor")), .Names = c("TPR", "FPR", "Classifier"), class = "data.frame", row.names = c(NA, 
    -400L))

colors = brewer.pal(7, "Dark2")[1:7]

ggplot(dat,aes(FPR,TPR,color=Classifier))+geom_line(size = 2, alpha = 0.7)+
      labs(title= "ROC curve",
           x = "False Positive Rate (1-Specificity)",
           y = "True Positive Rate (Sensitivity)") + scale_colour_manual(values=colors)

我想要做的是将对角线(Luck Mean / AUC)固定为黑色,并用虚线类型( - )固定为黑色。通常,我的意思是其他分类器可能会增加(例如AB / RF / SVM + ...),但运气总是固定的。我有办法修改上面的代码吗?

1 个答案:

答案 0 :(得分:4)

指定手动缩放时,可以使用命名的矢量值将颜色指定给不同的级别。这是一个例子

#set colors
colors = brewer.pal(7, "Dark2")[1:7]
colors<-setNames(colors[1:nlevels(dat$Classifier)], levels(dat$Classifier))
colors["Luck Mean/AUC = 0.500"]<-"black"

#set line types
ltys<-rep.int(1, nlevels(dat$Classifier))
ltys<-setNames(ltys, levels(dat$Classifier))
ltys["Luck Mean/AUC = 0.500"]<-2

您将看到colorsltys现在被命名为向量,为每个分类器指定颜色和线型。现在我们用

绘制那些
ggplot(dat,aes(FPR,TPR,color=Classifier, linetype=Classifier))+
    geom_line(size = 2, alpha = 0.7)+
    labs(title= "ROC curve",
         x = "False Positive Rate (1-Specificity)",
         y = "True Positive Rate (Sensitivity)") + 
    scale_colour_manual(values=colors) + 
    scale_linetype_manual(values=ltys)

获取

enter image description here

通过调整colorsltys向量中的名称和值,您应该可以自定义自己的喜好。