How to change labels (legends) in ggplot?

My code is similar to below, I want to change the ggplot label, but R always reminds me:

Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0 

What should I do?

 ggplot(mat,aes(x=sales,col=type))+ geom_density()+labels("red_sold","blue_sold","yellow_sold") 
0
source share
1 answer

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.

+3
source

Source: https://habr.com/ru/post/1208698/


All Articles