使用 ggplot 绘制两个独立因素的 cumsum

Using ggplot to plot cumsum of two separate factors

使用以下数据,我需要绘制因子 "in" 和“out.

的累积曲线
years <- c(1969, 1972, 1974, 1975, 1975, 1976, 1976, 1977, 1978, 1978, 1979, 1979, 1980, 1981, 1981, 1982, 1983, 1983, 1984, 1984, 1985, 1985, 1986, 1986, 1987, 1987, 1988, 1988, 1989, 1989, 1990, 1990, 1991, 1991, 1992)
places <- c("in","out","out","in","out","in","out","out","in","out","in",     "out","out","in","out","out","in","out","in","out","in","out","in", "out","in","out","in","out","in","out","in","out","in","out","in")   
count <- c(1,2,1,1,4,1,1,1,1,1,1,3,3,1,7,4,4,5,1,3,5,3,4,6,7,3,2,6,4,3,6,11,5, 7,9)

peryear <- data.frame(years,places,count)

如果我使用

绘图
ggplot(peryear %>% filter(places=="in"),aes(x=years,y=cumsum(count))) + geom_point()+geom_line()
ggplot(peryear %>% filter(places=="out"),aes(x=years,y=cumsum(count))) + geom_point()+geom_line()

我得到了我所期望的:

但是,当我尝试将 ggplot2 与

一起使用时
ggplot(peryear,aes(x=years,y=cumsum(count),color=places)) + geom_line()+geom_point()

我得到了以下错误的情节:

我认为这个图是错误的,因为两个地方的曲线都达到了高于 100 的值,而对于个别地块,它们达到了 70 左右。

如何使用 ggplot2 绘制数据的累积曲线?

最后一张图与您想要的很接近,但累积总和线之间似乎太接近了。这是一种处理方法:

ggplot() + geom_line(aes(x=years,y=cumsum(count),colour='red'),peryear %>% filter(places=='in')) +
    geom_line(aes(x=years,y=cumsum(count), colour='navy'),peryear %>% filter(places=='out')) +
    scale_colour_discrete(name  = 'places',
                  labels=c("in", "out"))