Is mat$type factor? If not, it will cause an error. Also, you cannot use labels(...) in this way.
Since you have not provided any data, here is an example of using the mtcars built-in dataset.
ggplot(mtcars, aes(x=hp,color=factor(cyl)))+ geom_density()+ scale_color_manual(name="Cylinders", labels=c("4 Cylinder","6 Cylinder","8- Cylinder"), values=c("red","green","blue"))

In this example
ggplot(mtcars, aes(x=hp,color=cyl))+...
will result in the same error you get because mtcars$cyl not a factor.
source share