设置颜色标签并改变qplot中的调色板 [英] Setting the color label and varying the color palette in qplot
问题描述
-
使用下面的代码,我可以为x和y轴设置标签,但不能为
cyl
在这里。 中的操作:x < - runif(100)
y <-runif(100)
time <-runif(100)$ b $ (pal)< - colorRampPalette(c('white','black'))
cols < - pal(10)[as.numeric(cut(time,breaks = 10))]
plot (x,y,pch = 19,col = cols)
您可以使用 scale_colour_continuous
来完成这两项任务。
<$ p
$ scale_colour_continuous(mpg,wt,data = mtcars,color = cyl,xlab =MPG name =Cylinders,low =white,high =black)
name
参数是色标的标签。参数低
和高
表示连续色阶的上限和下限。
如果您想用三种颜色指定连续色阶,可以使用
scale_colour_gradient2
: qplot(mpg,wt,data = mtcars,color = cyl,xlab =MPG,ylab =WT)+
scale_colour_gradient2(name =Cylinders,midpoint = median(mtcars $ cyl),
low =red,mid =green,high =black)
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")
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)
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.
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")
这篇关于设置颜色标签并改变qplot中的调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!