改变使用 ggplot2 创建的散点图的颜色渐变

发布于 2024-11-02 07:37:40 字数 775 浏览 1 评论 0原文

是否可以根据审美改变绘图的颜色渐变?我正在使用与下面所示的行类似的代码生成一个图,并发现在某些情况下,区分各个组并不总是很容易。例如,在下面的图表中,如果我可以让 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",
      "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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-11-09 07:37:40

您可以通过在调用 ggplot2 之前自行准备颜色来做到这一点。
这是一个例子:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文