简体   繁体   English

设置颜色标签并在qplot中更改调色板

[英]Setting the color label and varying the color palette in qplot

  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. 使用下面的代码,我可以为x和y轴设置标签,但在这里不能为cyl设置颜色。 The documentation provides no way around. 文档无法解决。

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

在此处输入图片说明

  1. How can I vary the color palette herein qplot ? 如何在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) 

You can use scale_colour_continuous for both tasks. 您可以对两个任务都使用scale_colour_continuous

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. 在这里, name参数是色标的标签。 The parameters low and high denote the lower and upper limits of the continuous colour scale. 参数lowhigh表示连续色标的下限和上限。

在此处输入图片说明


If you want to specify a continuous colour scale with three colours, you can use scale_colour_gradient2 : 如果要指定具有三种颜色的连续色标,可以使用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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM