改变使用ggplot2创建的散点图上的颜色渐变 [英] Vary the color gradient on a scatter plot created with ggplot2

查看:4490
本文介绍了改变使用ggplot2创建的散点图上的颜色渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过审美来改变情节的颜色渐变?我使用类似于下面提供的代码的代码生成一个阴谋,并在某些情况下发现,区分各个群体并不总是很容易。例如,在下面的图表中,如果我可以让A组点使用白蓝色渐变,B组点使用白红色渐变,则可以更容易地区分结果。

  data < -  data.frame(x = c(1,2,3,4,5,6,1,2,3,4,5,6 ),
y = c(1,2,3,4,5,6,1,2,3,4,5,6),grp = c(rep(A,6),rep( B,6)),
dt = c(2010-06-30,2010-05-31,2010-04-30,
2010-03-31 ,2010-02-26,2010-01-29,2010-06-30,
2010-05-31,2010-04-30,

p < - ggplot(data,aes(x,y,color = as.integer(as.Date (数据$ dt)))+
geom_jitter(size = 4,alpha = 0.75,aes(shape = grp))+
scale_colour_gradient(limits = as.integer(as.Date(c( 2010-01-29,2010-06-30))),
low =white,high =blue)+
scale_shape_discrete(name =)+
opts(legend.position =none)
print(p)


解决方案

你可以通过准备颜色来做到这一点
在调用ggplot2之前,你自己可以自己做一下。

下面是一个例子:

$ $ $ $ $ $ $ sdt< - rescale (as.numeric(as.Date(data $ dt)))#data scaled [0,1]
cols <-c(red,blue)每个组的梯度颜色

#这里计算每个值的颜色
data $ col < - ddply(data,。(grp),function(x)
data.frame(col = apply( colorRamp(c(white,cols [as.numeric(x $ grp)[1]]))(x $ sdt),
1,function(x)rgb(x [1],x [2 ],x [3],max = 255)))
)$ col

p < - ggplot(data,aes(x,y,shape = grp,color = col)) +
geom_jitter(size = 4,alpha = 0.75)+
scale_colour_identity()+#使用标识色阶
scale_shape_discrete(name =)+
opts(legend.position =none)
print(p)


Is it possible to vary a plot's color gradient by aesthetic? I'm generating a plot using code similar the lines presented below and finding in some cases that it is not always easy to distinguish between the various groups. For example, on the chart below it would be easier to distinguish the results if I could have the group A points use a white-blue gradient and the group B points use a white-red gradient.

data <- data.frame(x=c(1,2,3,4,5,6,1,2,3,4,5,6), 
    y=c(1,2,3,4,5,6,1,2,3,4,5,6), grp=c(rep("A",6),rep("B",6)),
    dt=c("2010-06-30","2010-05-31","2010-04-30",
      "2010-03-31","2010-02-26","2010-01-29","2010-06-30",
      "2010-05-31","2010-04-30",
      "2010-03-31","2010-02-26","2010-01-29"))
p <- ggplot(data, aes(x,y,color=as.integer(as.Date(data$dt)))) + 
    geom_jitter(size=4, alpha=0.75, aes(shape=grp)) + 
    scale_colour_gradient(limits=as.integer(as.Date(c("2010-01-29","2010-06-30"))),
    low="white", high="blue") +
    scale_shape_discrete(name="") +
    opts(legend.position="none")
print(p)

解决方案

you can do that by preparing color by yourself before calling ggplot2.
Here is an example:

data$sdt <- rescale(as.numeric(as.Date(data$dt)))  # data scaled [0, 1]
cols <- c("red", "blue") # colour of gradients for each group

# here the color for each value are calculated
data$col <- ddply(data, .(grp), function(x)
     data.frame(col=apply(colorRamp(c("white", cols[as.numeric(x$grp)[1]]))(x$sdt),
       1,function(x)rgb(x[1],x[2],x[3], max=255)))
       )$col

p <- ggplot(data, aes(x,y, shape=grp, colour=col)) +
  geom_jitter(size=4, alpha=0.75) + 
  scale_colour_identity() +  # use identity colour scale
  scale_shape_discrete(name="") +
  opts(legend.position="none")
print(p)

这篇关于改变使用ggplot2创建的散点图上的颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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