R ggplot多系列曲线 [英] R ggplot multiple series curved line

查看:104
本文介绍了R ggplot多系列曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制一个图上的多个数据系列。



我的数据如下所示:

  count_id AMV Hour duration_in_traffic AMV_norm 
1 16012E 4004 14 99 0
2 16012E 4026 12 94 22
3 16012E 4099 15 93 95
4 16012E 4167 11 100 163
5 16012E 4239 10 97 235

我正在用R绘制:

  ggplot(td_results,aes(AMV,duration_in_traffic))+ geom_line(aes(color = count_id))

这给了我:



然而,不是直线连接点我想弯曲。

我发现了下面的问题,但得到了意想不到的结果。



如何用某种形式的高阶多项式得到一条曲线?

解决方案

正如@MrFlick在评论中提到的那样,有一些严重的统计方法可以获得曲线,这可能与此处无关。



如果你只是想让你的图看起来更好然而,你可以尝试用 spline 来插入数据,然后将其作为另一个图层添加。

一些样条数据,使用了10倍的数量数据点(您可以根据需要增加或减少):

  library(dplyr)
dat2< - td_results%>%select(count_id,AMV,duration_in_traffic)%>%
group_by(count_id)%>%
do(as.data.frame(spline(x =。[[ AMV]],y =。[[duration_in_traffic]],n = nrow(。)* 10)))

然后我们使用原始数据绘制点,然后使用样条线数据(dat2)中的线:

  ggplot(td_results,aes(AMV,duration_in_traffic))+ 
geom_point(aes(color = factor(count_id)))+
geom_line(data = dat2, aes(x = x,y = y,color = factor(count_id)))

以下来自测试数据的图表:


I am plotting multiple series of data on one plot.

I have data that looks like this:

count_id    AMV Hour    duration_in_traffic AMV_norm
1   16012E  4004    14  99  0
2   16012E  4026    12  94  22
3   16012E  4099    15  93  95
4   16012E  4167    11  100 163
5   16012E  4239    10  97  235

I am plotting in R using:

ggplot(td_results, aes(AMV,duration_in_traffic)) + geom_line(aes(colour=count_id))

This is giving me:

However, rather than straight lines linking points I would like curved.

I found the following question but got an unexpected output. Equivalent of curve() for ggplot

I used: ggplot(td_results, aes(AMV,duration_in_traffic)) + geom_line(aes(colour=count_id)) + stat_function(fun=sin)

Thus giving:

How can I get a curve with some form of higher order polynomial?

解决方案

As @MrFlick mentions in the comments, there are serious statistical ways of getting curved lines, which are probably off topic here.

If you just want your graph to look nicer however, you could try interpolating your data with spline, then adding it on as another layer.

First we make some spline data, using 10 times the number of data points you had (you can increase or decrease this as desired):

library(dplyr)
dat2 <- td_results %>% select(count_id, AMV, duration_in_traffic) %>%
                group_by(count_id) %>%
                do(as.data.frame(spline(x= .[["AMV"]], y= .[["duration_in_traffic"]], n = nrow(.)*10)))

Then we plot, using your original data for points, but then using lines from the spline data (dat2):

library(ggplot2)
ggplot(td_results, aes(AMV, duration_in_traffic)) +
   geom_point(aes(colour = factor(count_id))) +
   geom_line(data = dat2, aes(x = x, y = y, colour = factor(count_id)))

This gives me the following graph from your test data:

这篇关于R ggplot多系列曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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