Filling in the area under a line graph in ggplot2: geom_area()

前端 未结 1 1331
深忆病人
深忆病人 2021-01-11 09:18

For the data:

    def.percent period  valence
1   6.4827843   1984-1985   neg
2   5.8232425   1985-1986   neg
3   -2.4003260  1986-1987   pos
4   -3.5994399          


        
相关标签:
1条回答
  • 2021-01-11 09:36

    This happens because in your case period is a categorical i.e. a factor variable. If you convert it to numeric it works fine:

    Data

    df <- read.table(header=T, text='  def.percent period  valence
    1   6.4827843   1984   neg
    2   5.8232425   1985   neg
    3   -2.4003260  1986   pos
    4   -3.5994399  1987   pos')
    

    Solution

    ggplot(df, aes(x=period, y=def.percent)) +
      geom_area(aes(fill=valence)) +
      geom_line() + geom_point() + geom_hline(yintercept=0)
    

    Plot

    enter image description here

    0 讨论(0)
提交回复
热议问题