[英]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.现在我想将其标准化为绿线(或平均值)的起始值,因此所有变量都从 Y 轴的同一点开始。 Similar to this:
与此类似:
Thanks for any help.谢谢你的帮助。
You could subtract the starting value of each vector depending on variable
-value using by()
.您可以使用
by()
根据variable
-value 减去每个向量的起始值。
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()
group_by()
and mutate()
to shift each variable by its initial y-value.group_by()
和mutate()
按其初始 y 值移动每个变量。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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.