How to change labels (legends) in ggplot?

前端 未结 2 1021
臣服心动
臣服心动 2021-01-16 03:13

My code is like below, I want to change the label of the ggplot, but R always remind me:

Error in unit(tic_pos.c, \"mm\") : \'x\' and \'units\' must have len         


        
2条回答
  •  情深已故
    2021-01-16 04:03

    Is mat$type a factor? If not, that will cause the error. Also, you can't use labels(...) this way.

    Since you did not provide any data, here's an example using the built-in mtcars 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))+...
    

    would cause the same error that you are getting, because mtcars$cyl is not a factor.

提交回复
热议问题