Reputation: 335
I have a trouble by setting the legend colors on my plot, I just can not to set the appropriate colors on my legend and all three appear red: Multiple Scatter ggplot. I tried using scale_colour_manual
options and scale_fill_manual
but it does not work. I do not know if the data frame and the code in gerenarl are appropiated but it were the only way I found for putting different regression lines on the same plot.
Here is a 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)
`
How can I configure the colors so they appear in the way that explain?
Upvotes: 1
Views: 452
Reputation: 206167
Here is a solution
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")
Note that you weren't using a fill really so I got rid of those. I moved the color into the aes()
to get a legend. Then I also used a scale_color_identity
to use the literal color values and rename them in the legend via the labels=
parameter.
Upvotes: 1
Reputation: 450
ggplot syntax is more suited to long data format - if you reshape the data frame, it should solve the problem and make the code simpler. Like this:
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"))
All aesthetics were moved to main ggplot call. Then both point
and smooth
use the same grouping and colors (i.e. by the mode
column) without writing it twice. Since we are using the inbuilt color scale, there is no need to explicitly specify any scales or colors for each group.
Note also that I reordered the ABS types when making the dataframe.
Upvotes: 2