Skip to content
RDocumentation: scale_colour_manual
  • AI Chat
  • Code
  • Report
  • Note that this notebook was automatically generated from an RDocumentation page. It depends on the package and the example code whether this code will run without errors. You may need to edit the code to make things work.

    if(!require('ggplot2')) {
        install.packages('ggplot2')
        library('ggplot2')
    }
    p <- qplot(mpg, wt, data = mtcars, colour = factor(cyl))
    
    p + scale_colour_manual(values = c("red","blue", "green"))
    p + scale_colour_manual(
      values = c("8" = "red","4" = "blue","6" = "green"))
    # With rgb hex values
    p + scale_colour_manual(values = c("#FF0000", "#0000FF", "#00FF00"))
    
    # As with other scales you can use breaks to control the appearance
    # of the legend
    cols <- c("8" = "red","4" = "blue","6" = "darkgreen", "10" = "orange")
    p + scale_colour_manual(values = cols)
    p + scale_colour_manual(values = cols, breaks = c("4", "6", "8"))
    p + scale_colour_manual(values = cols, breaks = c("8", "6", "4"))
    p + scale_colour_manual(values = cols, breaks = c("4", "6", "8"),
      labels = c("four", "six", "eight"))
    
    # And limits to control the possible values of the scale
    p + scale_colour_manual(values = cols, limits = c("4", "8"))
    p + scale_colour_manual(values = cols, limits = c("4", "6", "8", "10"))