使用ggplot绘制两个独立因子的集合

时间:2018-02-06 18:41:03

标签: r ggplot2 cumsum

使用以下数据我需要绘制因子“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()

我得到了我的期望:

cumsum of "in" cumsum of "out"

但是,当我尝试将ggplot2与

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

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

wrong cumsum ggplot

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

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

1 个答案:

答案 0 :(得分:2)

最后一张图表接近你想要的,但累积的总和线看起来彼此太近了。这是接近它的一种方法:

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"))

enter image description here