3
  1. Using the code below, i can set the labels for x and y axis, but can't set the label for color that is cyl here. The documentation provides no way around.

    qplot(mpg, wt, data=mtcars, colour=cyl,xlab="MPG",ylab="WT")
    

enter image description here

  1. How can I vary the color palette herein qplot? So, I wish to do something like in the code below:

    x <- runif(100)
    y<-runif(100)
    time<-runif(100)  
    pal <- colorRampPalette(c('white','black'))
    cols <- pal(10)[as.numeric(cut(time,breaks = 10))]
    plot(x,y,pch=19,col = cols)
    
Community
  • 1
  • 1
Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142

1 Answers1

4

You can use scale_colour_continuous for both tasks.

library(ggplot2)
qplot(mpg, wt, data = mtcars, colour = cyl, xlab = "MPG", ylab = "WT") +
  scale_colour_continuous(name = "Cylinders", low = "white", high = "black")

Here, the name parameter is the label for the colour scale. The parameters low and high denote the lower and upper limits of the continuous colour scale.

enter image description here


If you want to specify a continuous colour scale with three colours, you can use scale_colour_gradient2:

qplot(mpg, wt, data = mtcars, colour = cyl, xlab = "MPG", ylab = "WT") +
  scale_colour_gradient2(name = "Cylinders", midpoint = median(mtcars$cyl),
                         low = "red", mid = "green", high = "black")

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168