我正在尝试绘制散点图,其中每个点属于一个组,并且每个组都有一个颜色。不幸的是,使用我正在使用的代码时,颜色无法正确显示:
library(ggplot2)
df <- read.csv(text="Name,Group,Cat,Bet,InD,OutD,Close,Color
a,ANC,1,0.0465824668474985,13,15,0.5128205128205128,#ABA6A4
b,WF,1,0.006805116033755275,31,11,0.625,#BAC761
c,YC,2,0.016471518987341775,10,12,0.5263157894736842,#779BC0
d,TH,2,0.004483122362869198,10,3,0.5333333333333333,#8AB4A2
e,FL,3,0.13609064195298373,15,21,0.5882352941176471,#803C72
f,MJ,2,0.000959915611814346,2,7,0.48484848484848486,#A94D6F
g,AFPA,1,0.0057674050632911394,15,12,0.5517241379310345,#6D482E
h,PC,4,0.04844409282700422,15,24,0.5970149253731343,#80A65D
i,PC,4,0.014266877637130802,3,7,0.5063291139240507,#80A65D
", check.names = FALSE, sep = ",")
ggplot(df, aes(InD, OutD)) +
geom_point(aes(color=factor(Group), size = 5)) +
scale_colour_manual(values= levels(df$Color))
我显然做错了什么......在散点图中使用自定义单个颜色的方法是什么?
答案 0 :(得分:2)
将values
指定为命名向量可能会更好:
+ scale_colour_manual(values= setNames(as.character(df$Color),df$Group))