当我在同一个图中绘制2个数据帧时,我无法在这里得到相同的原点--我的数据样本:
spec_sum <- data.frame(x = c("2015-01-08","2015-01-13","2015-01-14","2015-01-15","2015-01-15","2015-01-19","2015-01-19","2015-01-21","2015-01-21","2015-01-27"),y = c(1, 1, 1, -1, -1, 1, 1, 1, 1, 1))
odtgen_sum <- data.frame(x = c("2015-01-12","2015-01-14","2015-01-15","2015-01-26","2015-01-27","2015-01-29","2015-01-30","2015-01-30","2015-02-04","2015-02-04"),y=c(1,-1,1,1,1,1,-1,-1,-1,1))
在这里,我的ggplot代码:
library(reshape)
newData <- melt(list(spec_sum = spec_sum, odtgen_sum = odtgen_sum),
id.vars = "x")
#Specify colour vector
cols <- c("blue", "red", "green", "orange")
ggplot(newData, aes(x, cumsum(value), colour = L1)) +
geom_line() + xlab("Date") + ylab("Nb depeches dev") +
scale_colour_manual(values = cols) + theme_bw() +
theme(legend.justification=c(0,0), legend.position=c(0.8,0))
示例
发布于 2016-02-14 19:17:39
我猜,这是因为在您的示例中,cumsum
不聚合每个组。试着事先计算一下:
# create toy data
set.seed(1123)
n <- 100
df <- data.frame(f=gl(2, n, lab=letters[1:2]),
x=rep(1:n, 2),
y=rbinom(2*n, 1, .5))
# add cumulative sum
df$y_cum <- ave(df$y, df$f, FUN=cumsum)
# plot
ggplot(df, aes(x, y_cum, color=f)) + geom_line()
**你没有提供一个可重复的例子。
https://stackoverflow.com/questions/35393216
复制相似问题