如何在R和ggplot2中获得多个数据集的单一趋势线? [英] How to get a single trendline with multiple data sets in R and ggplot2?

查看:407
本文介绍了如何在R和ggplot2中获得多个数据集的单一趋势线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

三十年来,我拥有多种数据来源。
数据不连续,并且在多个地方重叠。我想用不同的颜色为每个数据源绘制点,但添加一个使用所有数据源的趋势线。
包含的代码有一些示例数据和两个绘图示例。第一次调用ggplot,绘制所有数据的单一趋势线。第二个ggplot调用,用各自的趋势线以不同的颜色清晰地绘制每个源。

  library(ggplot2)
the.data< - read.table(header = TRUE,sep =, ,
text =来源,年份,价值
S1,1976,56.98
S1,1977,55.26
S1,1978,68.83
S1,1979,59.70
S1,1980,57.58
S1,1981,61.54
S1,1982,48.65
S1,1983,53.45
S1,1984,45.95
S1 ,1985,51.95
S1,1986,51.85
S1,1987,54.55
S1,1988,51.61
S1,1989,52.24
S1,1990,49.28
S1,1991,57.33
S1,1992,51.28
S1,1993,55.07
S1,1994,50.88
S2,1993,54.90
S2, 1994,51.20
S2,1995,52.10
S2,1996,51.40
S3,2002,57.95
S3,2003,47.95
S3,2004,48.15
S3,2005,37.80
S3,2006,56.96
S3,2007,48.91
S3,2008,44.00
S3,2009,45.35
S3,2010 ,49.40
S3,2011,51.19)
ggplot(the.data,aes(the.data $ year,the.data $ value))+ geom_point()+ geom_smooth()
#ggplot(the.data,aes(the.data $ year,the.data $ value,color = the.data $ source))+ geom_point()+ geom_smooth()

第二次调用显示彩色数据点,我想添加一个代表所有年份的连续趋势线。

解决方案

像这样:

  ggplot (aes(group = 1))



一些注释:


  • 不要映射美学到像 the.data $ year 这样的独立向量。 (直到你真的知道你在做什么,并知道何时打破该规则。)只需使用列名即可。


  • 映射您想要的美学在它们各自的 geom 调用中的不同层中。在这种情况下,我希望点的着色方式不同,但对于流畅的线条,我希望将所有数据组合在一起( group = 1 )。



I have multiple sources of data over three decades. The data is discontiguous and overlaps in multiple places. I would like to plot the points for each data source in a different color but then add a single trendline that uses all of the data sources. The included code has some sample data and two plot examples. The first call to ggplot, plots a single trendline for all of the data. the second ggplot call, plots each source distinctly in different colors with its own trendline.

    library(ggplot2)
    the.data <- read.table( header=TRUE, sep=",", 
    text="source,year,value
    S1,1976,56.98
    S1,1977,55.26
    S1,1978,68.83
    S1,1979,59.70
    S1,1980,57.58
    S1,1981,61.54
    S1,1982,48.65
    S1,1983,53.45
    S1,1984,45.95
    S1,1985,51.95
    S1,1986,51.85
    S1,1987,54.55
    S1,1988,51.61
    S1,1989,52.24
    S1,1990,49.28
    S1,1991,57.33
    S1,1992,51.28
    S1,1993,55.07
    S1,1994,50.88
    S2,1993,54.90
    S2,1994,51.20
    S2,1995,52.10
    S2,1996,51.40
    S3,2002,57.95
    S3,2003,47.95
    S3,2004,48.15
    S3,2005,37.80
    S3,2006,56.96
    S3,2007,48.91
    S3,2008,44.00
    S3,2009,45.35
    S3,2010,49.40
    S3,2011,51.19") 
    ggplot( the.data, aes( the.data$year, the.data$value ) ) + geom_point() + geom_smooth()
    #ggplot( the.data, aes( the.data$year, the.data$value, color=the.data$source ) ) + geom_point() + geom_smooth()

The second call displays the colored data points and I would like to add a single contiguous trendline representing all of the years.

解决方案

Like this:

ggplot(the.data, aes( x = year, y = value ) ) + 
    geom_point(aes(colour = source)) + 
    geom_smooth(aes(group = 1))

A few notes:

  • Don't map aesthetics to an isolated vector like the.data$year. (Until you really know what you're doing, and know when to break that rule.) Just use the column names.

  • Map the aesthetics that you want in separate layers in their respective geom calls. In this case, I want the points colored differently, but for the smooth line, I want the data grouped all together (group = 1).

这篇关于如何在R和ggplot2中获得多个数据集的单一趋势线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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