简体   繁体   中英

How to normalize data series to start value = 0?

I have a dataset similar to this:

library(ggplot2)
data(economics_long)
economics_long$date2 <- as.numeric(economics_long$date) + 915
ggplot(economics_long, aes(date2, value01, colour = variable)) +
       geom_line()

Which gives the following plot:

在此处输入图片说明

Now I would like to normalize it to the start value of the green line (or the mean), so all variables start at the same point of the Y axes. Similar to this:

在此处输入图片说明

Thanks for any help.

You could subtract the starting value of each vector depending on variable -value using by() .

library(ggplot2)
l <- by(economics_long, economics_long$variable, function(x) 
  within(x, varnorm <- value01 - value01[1]))
dat <- do.call(rbind, l)

ggplot(dat, aes(date2, value01.n, colour = variable)) +
  geom_line()

在此处输入图片说明

use group_by() and mutate() to shift each variable by its initial y-value.

library(tidyverse)
data(economics_long)

economics_long %>% 
  group_by(variable) %>% 
  mutate(value_shifted = value01 - value01[1]) %>% 
  ungroup() %>% 
  ggplot(aes(date2, value_shifted, colour = variable)) +
  geom_line()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM