绘制具有已知误差的时间序列(ggplot2) [英] Plot time series with known error (ggplot2)

查看:111
本文介绍了绘制具有已知误差的时间序列(ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用美国社区调查(ACS)对特定位置进行为期1年的估计,这需要几年的时间.例如,我试图绘制骑自行车上班的男女比例随时间的变化情况.通过ACS,我可以获得估算值和标准误,然后可以使用它们计算估算值的上下限.

I'm working with American Community Survey (ACS) 1-year estimates for a specific location over several years. For example, I'm trying to plot how the proportion of men and women riding a bike to work changes over time. From the ACS, I get estimates and standard error, which I can then use to calculate the upper and lower bounds of the estimates.

所以宽格式的简化数据结构是这样的:

So the simplified data structure in wide format is like this:

| Year | EstimateM | MaxM | MinM | EstimateF | MaxF | MinF |
|------|-----------|------|------|-----------|------|------|
| 2005 | 3.0       | 3.5  | 2.5  | 2.0       | 2.3  | 1.7  |
| 2006 | 3.1       | 3.5  | 2.6  | 2.0       | 2.3  | 1.7  |
| 2007 | 5.0       | 4.2  | 5.8  | 2.5       | 3.0  | 2.0  |
| ...  | ...       | ...  | ...  | ...       | ...  | ...  |

如果我只想绘制估计值,我将仅使用两个Estimate变量作为measure.vars

If I only wanted to plot the estimates, I'd melt the data with only the two Estimate variables as measure.vars

GenderModeCombined_long <- melt(GenderModeCombined,
                            id = "Year",
                            measure.vars = c("EstimateM",
                                             "EstimateF")

然后可以使用ggplot2

ggplot(data=GenderModeCombined_long,
  aes(x=year, y=value, colour=variable)) +
  geom_point() +
  geom_line()

这会产生一个像这样的图

This produces a graph like so

(对不起,没有足够的代表来发布图片)

(sorry, don't have enough rep to post images)

我遇到的问题是如何在两个估算图中添加误差线.我可以将它们作为measure vars添加到已融化的数据集中,但是然后如何告诉ggplot应该将哪些内容绘制为值,将哪些内容绘制为误差线呢?我是否必须仅使用最小/最大数据创建一个单独的数据帧,然后分别加载?

Where I'm stuck is how to add error bars to the two estimate graphs. I could add them as measure vars to the melted dataset, but then how do I tell ggplot what should be plotted as values and what as error bars? Do I have to create a separate data frame with just the min/max data and then load that separately?

geom_errorbar(data = errordataMmax, aes(ymax = ??, ymin = ??)) 

我感觉自己正在以某种错误的方式进行处理和/或以错误的方式设置了数据.

I have the feeling that I'm somehow approaching this the wrong way and/or have my data set up the wrong way.

推荐答案

欢迎使用SO.这里的问题是您有三个显式"变量(估算值, 最小值和最大值)和一个以列名称编码的隐式"(性别).解决此问题的一种方法是使性别"成为显式的分组变量.转到长格式后,创建一个性别"变量,从键列(变量)中删除性别指示,然后返回宽格式. 这样的事情会起作用:

Welcome to SO. The problem here is that you have three "explicit" variables (Estimate, Min and Max) and an "implicit" one (gender) which is coded in column names. A way to solve this is to make "gender" an explicit grouping variable. After you go to long format, create a "gender" variable, remove the indication of gender from the key column (variable) and then go back to wide format. Something like this would work:

library(ggplot2)
library(dplyr)
library(tidyr)
library(tibble)

GenderModeCombined <- tibble::tribble(
  ~Year,   ~EstimateM,   ~MaxM,   ~MinM,   ~EstimateF,   ~MaxF,   ~MinF,  
  2005,         3.0,    3.5,    2.5,         2.0,    2.3,    1.7,  
  2006,         3.1,    3.5,    2.6,         2.0,    2.3,    1.7,  
  2007,         5.0,    4.2,    5.8,         2.5,    3.0,    2.0
)

GenderModeCombined.long <- GenderModeCombined %>% 
  # switch to long format
  tidyr::gather(variable, value, -Year,  factor_key = TRUE) %>% 
  # add a gender variable
  dplyr::mutate(gender   = stringr::str_sub(variable, -1)) %>% 
  # remove gender indication from the key column `variable`
  dplyr::mutate(variable = stringr::str_sub(variable, end = -2)) %>%
  # back to wide format
  tidyr::spread(variable, value)

GenderModeCombined.long
#> # A tibble: 6 x 5
#>    Year gender Estimate   Max   Min
#>   <dbl> <chr>     <dbl> <dbl> <dbl>
#> 1  2005 F           2     2.3   1.7
#> 2  2005 M           3     3.5   2.5
#> 3  2006 F           2     2.3   1.7
#> 4  2006 M           3.1   3.5   2.6
#> 5  2007 F           2.5   3     2  
#> 6  2007 M           5     4.2   5.8

ggplot(data=GenderModeCombined.long,
       aes(x=Year, y=Estimate,colour = gender)) +
  geom_point() +
  geom_line() + 
  geom_errorbar(aes(ymax = Max, ymin = Min))  

reprex软件包(v0.2.1)于2018年12月29日创建

Created on 2018-12-29 by the reprex package (v0.2.1)

这篇关于绘制具有已知误差的时间序列(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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