我试图在3代中显示13种不同程序的灵敏度和选择性的变化。这是程序1和2的数据,例如:
Sensitivity Selectivity Program Generation
0.317 0.347 1 1
0.282 0.301 2 1
0.496 0.118 1 2
0.339 0.176 2 2
0.525 0.050 1 3
0.397 0.131 2 3
我想要一个散点图,其中的线条连接给定程序的点(每个程序3个点,每一个一个)和每个程序的不同颜色,并且点的轮廓颜色与该程序的行颜色相同但由生成定义的填充。
一些点非常接近,生成点最终重叠一点,所以如果将点轮廓和填充映射在一起而不是为程序创建一个层并为子代覆盖另一个层将更清楚。所以我使用的形状可以定义轮廓和填充(pch = 21)。我越来越接近我希望情节看起来如何:
dataset=read.csv("10cmpds_mammal_mean_allgens.csv",h=T)
Generation<-factor(dataset$Generation)
Program<-factor(dataset$Program)
ggplot()+
geom_line(data=dataset,aes(x=Sensitivity,y=Selectivity,color=factor(Program)),size=1)+
geom_point(data=dataset, aes(x=Sensitivity, y=Selectivity, color=factor(Program)), pch=21, col=factor(Program), bg=factor(Generation),size=6)+
scale_color_manual(name="", values=c("#D42515","#D49A95","#DD7310","#F0CD1E","#0FB135","#0B78CC","#679EC9","#353535","#505050","#7B7A7C","#B4B4B4","#8348CF","#A787D1"))
这会产生正确的效果,但我无法弄清楚如何更改填充颜色。向scale_color_manual
添加其他颜色定义不会影响填充颜色(此处列出了13种颜色,因为我绘制了13个程序)。如何根据生成手动定义填充颜色?
也许某种互动(例如,在此讨论:Coloring ggplot by multiple factors)会让我们更接近,但我不确切地知道如何使这项工作成功。
谢谢!
编辑补充说,一位同事提出了一种使用基础R包实现此目的的方法:
dataset=read.csv("10cmpds_mammal_mean_allgens.csv",h=T)
color_lines=c("blue", "orange","purple","green","red","gray","black","brown","yellow","tan","aquamarine","gold","purple")
legend_colors=c("bisque", "gray", "black", "blue","orange","purple","green","red","gray","black","brown","yellow","tan","aquamarine","gold","purple")
legend_names=c("Generation 1","Generation 2","Generation 3", "model1","model2","model3","model4","model5","model6","model7","model8","model9","model10","model11","model12","model13")
legend_shapes = c(16,16,16,15,15,15,15,15,15,15,15,15,15,15,15,15)
plot(1, type="n", xlab="Sensitivity", ylab="Selectivity", xlim=c(0, 0.75), ylim=c(0, 0.75))
for(i in 1:max(dataset[,3])){
program_select <- which(dataset[,3]==i)
xvals <- dataset[program_select,1]
yvals <- dataset[program_select,2]
lines(xvals,yvals, col=color_lines[i])
points(xvals,yvals, cex=2, pch=21, col=color_lines[i], bg=c("bisque","grey","black"))
}
legend("topright", legend_names, pch=legend_shapes, col=legend_colors, cex=0.75, x.intersp = 0.7, y.intersp = 0.6)
如果有可能我仍然希望在ggplot2中生成这个,只是为了使绘图可以轻松匹配其他绘图的样式。
答案 0 :(得分:1)
我想你想要
ggplot(data = dataset,
aes(x = Sensitivity, y = Selectivity,
color = factor(Program),
fill = factor(Generation),
group = factor(Program)))+
geom_line(size = 1) +
geom_point(shape = 21, size = 6) +
scale_color_manual(name = "",
values = c("#D42515", "#D49A95", "#DD7310",
"#F0CD1E", "#0FB135", "#0B78CC",
"#679EC9", "#353535", "#505050",
"#7B7A7C", "#B4B4B4", "#8348CF", "#A787D1")) +
scale_fill_manual(values = c("bisque", "grey", "black"))
如果您在原始ggplot()
调用(以及您的数据)中指定了美学映射,则无需为后续图层重复这些映射。我使用与填充颜色相同的颜色,这可能是一个糟糕的选择,但您可以将它们更改为正确的数字/值。
另外值得指出的是,难以记住的首字母缩略词如“pch”和“bg”在ggplot中毫无意义。 “pch”替换为shape
,“bg”替换为fill
(“{”代替color
)。
Ialsoaddedwhitepsaceasitimprovesreadability。
使用您在问题中提供的数据,这会产生以下图表