使用ggplot标签时出现问题

时间:2017-02-23 18:51:17

标签: r ggplot2

我在绘图上设置图例颜色时出现问题,我无法在图例上设置合适的颜色,并且所有三种颜色都显示为红色:Multiple Scatter ggplot。我尝试使用scale_colour_manual选项和scale_fill_manual,但它不起作用。我不知道数据框架和gerenarl中的代码是否合适,但这是我发现在同一个图上放置不同回归线的唯一方法。 这是一个MWE:

`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)
`

如何配置颜色以便它们以解释的方式显示?

1 个答案:

答案 0 :(得分: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