2017-02-23 75 views
1

我在设计图上的图例颜色时遇到了麻烦,我只是不能在我的图例上设置适当的颜色,并且所有三个都显示为红色:Multiple Scatter ggplot。我尝试使用scale_colour_manual选项和scale_fill_manual,但它不起作用。我不知道数据框和gerenarl中的代码是否合适,但这是我找到将不同回归线放在同一个图上的唯一方法。 这里是一个MWE:使用ggplot标签的问题

`library("ggplot2") 
ConcCurve<-c(0.000,5.809,11.514,21.995,32.349,44.390,53.552) 
ABSHei<-c(0.01299076, 0.44779044, 0.87251242, 1.64435113, 2.41385198, 3.25395864,3.93389333) 
ABSAr3<-c(0.0224455, 0.8303167, 1.6170380, 3.0466451, 4.4496162, 5.9631238, 7.1746112) 
ABSAr5<-c(0.03847996, 1.44915907, 2.81864550, 5.29479463, 7.69466231, 10.27269797, 12.32472597) 
DataR<-data.frame(ConcCurve,ABSHei,ABSAr3,ABSAr5) 
p1<-ggplot(DataR) + 
    geom_point(aes(x=ConcCurve,y=ABSHei,fill="Height"),colour="blue") + 
    geom_smooth(aes(ConcCurve,ABSHei), method="lm", se=T,level = 0.9999,lwd=0.6, col ="blue") + 
    geom_point(aes(x=ConcCurve,y=ABSAr3,fill = "Area 3 pix"),colour="green") + 
    geom_smooth(aes(ConcCurve,ABSAr3), method="lm", se=T,level = 0.9999,lwd=0.6, col ="green") + 
    geom_point(aes(x=ConcCurve,y=ABSAr5,fill = "Area5 pix"),colour="red")+ 
    geom_smooth(aes(ConcCurve,ABSAr5), method="lm", se=T,level = 0.9999,lwd=0.6, col ="red") + 
    labs(x = expression(paste(plain("Hg"^plain("2+"))," Concentration (",mu,"g ",plain("kg"^plain("-1")),")")), y = "Integrated absorbance")+ 
    ggtitle("Calibration curves obtained using R")+ 
    guides(fill = guide_legend(reverse=F,title="Evaluation\nmode"))+ 
    scale_colour_manual(labels=c("Heigth", "Area 3 pix", "Area 5 pix"), 
         breaks=c("Heigth", "Area 3 pix", "Area 5 pix"), 
         values=c("blue","green","red")) 
print(p1) 
` 

如何配置的颜色,使他们出现在解释的方式?

+0

似乎有在你的代码(例如“Heigth”)的一些错字,但他们似乎并不成为问题的唯一原因,如将它们固定不产生你想要的结果。 – wwl

+0

我也试过在这些问题中给出的解决方案,他们没有工作:http://stackoverflow.com/questions/19330257/changing-the-color-in-the-legend-with-ggplot2-in-r – wwl

回答

2

ggplot语法更适合于长期数据格式 - 如果你重塑数据帧,它应该解决的问题,使代码更简单。像这样:

library("ggplot2") 
ConcCurve<-c(0.000,5.809,11.514,21.995,32.349,44.390,53.552) 
ABSHei<-c(0.01299076, 0.44779044, 0.87251242, 1.64435113, 2.41385198, 3.25395864,3.93389333) 
ABSAr3<-c(0.0224455, 0.8303167, 1.6170380, 3.0466451, 4.4496162, 5.9631238, 7.1746112) 
ABSAr5<-c(0.03847996, 1.44915907, 2.81864550, 5.29479463, 7.69466231, 10.27269797, 12.32472597) 
DataR<-data.frame(ConcCurve, ABS=c(ABSAr5,ABSAr3,ABSHei), 
     mode=rep(c("Area 3 pix", "Area 5 pix", "Height"), each=7)) 

ggplot(DataR, aes(x=ConcCurve, y=ABS, color=mode)) + geom_point() + 
    geom_smooth(method="lm", level=0.9999, lwd=0.6) + 
    labs(x = expression(paste(plain("Hg"^plain("2+"))," Concentration (",mu,"g ",plain("kg"^plain("-1")),")")), 
     y = "Integrated absorbance") + 
    ggtitle("Calibration curves obtained using R") + 
    guides(guide_legend(title="Evaluation\nmode")) 

所有的美学都被转移到主ggplot调用。然后pointsmooth都使用相同的分组和颜色(即使用mode列),而不写入两次。由于我们使用内置色标,因此不需要为每个组明确指定任何比例或颜色。
另请注意,我在制作数据帧时重新排序了ABS类型。

enter image description here

+0

这是一个非常优雅的解决方案,我也尝试了数据帧重组,但当时并不奏效。谢谢 – CristhianParedes

1

这里是一个解决方案,你不使用填充真的让我摆脱了那些

ggplot(DataR) + 
    geom_point(aes(x=ConcCurve,y=ABSHei, color="blue")) + 
    geom_smooth(aes(ConcCurve,ABSHei), color="blue", method="lm", se=T,level = 0.9999,lwd=0.6) + 
    geom_point(aes(x=ConcCurve,y=ABSAr3, color="green")) + 
    geom_smooth(aes(ConcCurve,ABSAr3), color ="green", method="lm", se=T,level = 0.9999,lwd=0.6) + 
    geom_point(aes(x=ConcCurve,y=ABSAr5, colour="red"))+ 
    geom_smooth(aes(ConcCurve,ABSAr5), color ="red", method="lm", se=T,level = 0.9999,lwd=0.6) + 
    labs(x = expression(paste(plain("Hg"^plain("2+"))," Concentration (",mu,"g ",plain("kg"^plain("-1")),")")), y = "Integrated absorbance")+ 
    ggtitle("Calibration curves obtained using R")+ 
    guides(color = guide_legend(reverse=F,title="Evaluation\nmode"))+ 
    scale_colour_identity(labels=c("Height", "Area 3 pix", "Area 5 pix"), 
         breaks=c("blue", "green", "red"), guide="legend") 

注意。我将颜色移至aes()以获得图例。然后,我还使用scale_color_identity来使用文字颜色值,并通过参数labels=在图例中对其进行重命名。

enter image description here

+0

非常感谢,这是我在stackoverflow中的第一次体验,并且您的合适答案解决了我的问题,使我学习了一些关于ggplot2的知识,并以非常快的方式摆脱了这对我造成的压力。再次感谢:D – CristhianParedes

+0

@juod的答案是一个更好的长期解决方案。如果您的数据格式很整齐,ggplot更容易使用。如果可能的话,应该避免像这样添加多个图层。 – MrFlick