ggplot2的散点图按日期着色 [英] Scatter plot with ggplot2 colored by dates

查看:110
本文介绍了ggplot2的散点图按日期着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用日期标记颜色的散点图.目前,我正在执行以下操作,但是即使图形看起来像我想要的那样,我也无法找到一种以图例的易读格式获取日期的方法.例如,我尝试将其格式设置为20140101,但整年都在较小范围内,即<20141231和我一年之内没有得到不同的颜色.

I am trying to do a scatter plot with colored by dates. Currently I am doing the following but I have not been able to find a way to get the dates in a good readable format for the legend even though the graph looks the way I want it. I tried formatting them as 20140101 for example but the whole year falls within a small range i.e < 20141231 and I don't get different colors within the year.

data(cars)
cars['dt'] = seq(Sys.Date(),Sys.Date()-980,-20)

ggplot(cars,aes(speed,dist,colour = as.integer(dt))) + geom_point(alpha = 0.6) +
scale_colour_gradientn(colours=c('red','green','blue')) 

有人可以推荐解决方案吗?具体来说,我希望每个日期都是不同的颜色/阴影. (对于我的实际数据,我每天大约有5-6年的数据)

Can someone recommend a solution please? To be specific I would like every date to be a different color/shade of a color. (For my actual data I have about 5-6 years of daily data)

推荐答案

只需添加一个标签函数:

Just add a labeler function:

ggplot(cars,aes(speed,dist,colour=as.integer(dt))) + geom_point(alpha = 0.6) +
  scale_colour_gradientn(colours=c('red','green','blue'), labels=as.Date)

这篇关于ggplot2的散点图按日期着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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