设置颜色标签并改变qplot中的调色板 [英] Setting the color label and varying the color palette in qplot

查看:824
本文介绍了设置颜色标签并改变qplot中的调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 使用下面的代码,我可以为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)


  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")
    

  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)
    

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆