我一直在尝试绘制35个点的散点图,其中7组点(凸包)中的每一个都由多边形划分,具有其自己的手动定义的颜色(即7个填充/轮廓颜色)。另外,我试图用自己特定的手动定义颜色(即35种颜色)为35个点中的每一个着色。我已经复制了下面的代码,但是得到以下错误:“'颜色'的比例已经存在。为'颜色'添加另一个比例,这将取代 现有规模。“并且显然是意外的结果,因为这些点没有按照需要着色。这是我可重现的代码:
x_val<-c(0.05,0.10,-0.35,-0.20,-0.09,0.43,0.63,0.60, 0.41,0.02,-0.18,0.19,-0.22,0.49,0.20,-0.01,-0.37, -0.48,-0.24,0.21,0.29,-0.18,0.28,-0.22,0.10,0.00,-0.55,0.00,-0.39,-0.43,0.00,0.19,-0.16,0.32,-0.44)
y_val<-c(0.49,-0.40,0.22,0.20,0.03,-0.14,-0.06,0.16,0.20,-0.21,0.42,-0.04,0.13,-0.32,-0.52,0.49,-0.41,-0.13,-0.47,0.33,0.35,0.47,-0.24,0.05,-0.44,0.24,0.20,0.16,-0.22,-0.26,-0.10,-0.04,-0.34,0.13,0.06)
group<-c(4,3,4,6,6,1,1,1,1,3,2,4,6,1,3,6,7,7,5,6,6,6,3,2,3,2,7,6,5,5,6,6,5,6,5)
plot_num<-c(1:35)
my_data<-as.data.frame(cbind(x_val,y_val,group,plot_num),35,4)
plot_col<-c("#83FEA7","#8D1E47","#2BBCB3","#4DB623","#648900","#D460B4","#FF747B","#FAAA94","#D0B5CA","#7D4ED7","#51EEE7","#A079E7","#48A380","#E23195","#A30088","#75FF38","#271CD4","#1061C3","#430B7C","#A6D585","#B7DB50","#50FA64","#B54638","#4890F3","#8D13C0","#78C0FF","#00B584","#77AC2C","#224C41","#1C4278","#77683D","#A1790F","#542E2F","#BDA332","#199338")
group_col<-c("#E67AA1","#5BBFF3","#962786","#6FBBC0","#304552","#7EB440","#1266B4")
find_hull <- function(my_data) my_data[chull(my_data[,1], my_data[,2]), ]
hulls <- ddply(my_data, "group", find_hull)
FF<-ggplot(data = my_data, aes(x_val,y_val, colour=factor(group), fill = factor(group))) +
geom_polygon(data = hulls, alpha = 0.5) +
scale_fill_manual(values= group_col) +
scale_color_manual(values= group_col)
FF + geom_point(data = my_data, aes(x_val,y_val,colour=factor(plot_num),size=4))+
scale_color_manual(values=plot_col)
正如您所看到的,两个scale_color_manual调用冲突,因此多边形的轮廓和点都不会根据其手动定义的十六进制代码进行着色。 FF本身就很好,就像他们自己的点一样,例如:
ggplot(data = my_data, aes(x_val,y_val, colour=factor(plot_num))) +
geom_point(aes(size=4))+
scale_color_manual(values=plot_col)
但是当合并时,情节会爆炸并产生错误。感谢您的帮助
答案 0 :(得分:2)
我们只能使用scale_color_manual一次。所以我把代码绘制多边形框架分开来放置&#34; color = ~~&#34;外面的aes()。
x_val<-c(0.05,0.10,-0.35,-0.20,-0.09,0.43,0.63,0.60, 0.41,0.02,-0.18,0.19,-0.22,0.49,0.20,-0.01,-0.37, -0.48,-0.24,0.21,0.29,-0.18,0.28,-0.22,0.10,0.00,-0.55,0.00,-0.39,-0.43,0.00,0.19,-0.16,0.32,-0.44)
y_val<-c(0.49,-0.40,0.22,0.20,0.03,-0.14,-0.06,0.16,0.20,-0.21,0.42,-0.04,0.13,-0.32,-0.52,0.49,-0.41,-0.13,-0.47,0.33,0.35,0.47,-0.24,0.05,-0.44,0.24,0.20,0.16,-0.22,-0.26,-0.10,-0.04,-0.34,0.13,0.06)
group<-c(4,3,4,6,6,1,1,1,1,3,2,4,6,1,3,6,7,7,5,6,6,6,3,2,3,2,7,6,5,5,6,6,5,6,5)
plot_num<-c(1:35)
my_data<-as.data.frame(cbind(x_val,y_val,group,plot_num),35,4)
plot_col<-c("#83FEA7","#8D1E47","#2BBCB3","#4DB623","#648900","#D460B4","#FF747B","#FAAA94","#D0B5CA","#7D4ED7","#51EEE7","#A079E7","#48A380","#E23195","#A30088","#75FF38","#271CD4","#1061C3","#430B7C","#A6D585","#B7DB50","#50FA64","#B54638","#4890F3","#8D13C0","#78C0FF","#00B584","#77AC2C","#224C41","#1C4278","#77683D","#A1790F","#542E2F","#BDA332","#199338")
group_col<-c("#E67AA1","#5BBFF3","#962786","#6FBBC0","#304552","#7EB440","#1266B4")
find_hull <- function(my_data) my_data[chull(my_data[,1], my_data[,2]), ]
hulls <- ddply(my_data, "group", find_hull)
# make a color vector from group column
g.col <- group_col[hulls$group]
FF <- ggplot(data = my_data, aes(x_val,y_val)) +
geom_polygon(data = hulls, alpha = 0.5, aes(fill=factor(group))) + # fill
geom_polygon(data = hulls, alpha = 0, aes(group=factor(group)), colour=g.col) + # draw lines with specific color
scale_fill_manual(values= group_col) # change fill color.
## If you needn't legend, you'll use integrated code.
# geom_polygon(data = hulls, alpha = 0.5, aes(group=factor(group)), fill=g.col, colour=g.col)
FF + geom_point(data = my_data, aes(x_val, y_val, colour=factor(plot_num)), size=4) +
scale_color_manual(values=plot_col)