改变使用 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",
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过在调用 ggplot2 之前自行准备颜色来做到这一点。
这是一个例子:
you can do that by preparing color by yourself before calling ggplot2.
Here is an example: