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

[亡魂溺海] 提交于 2019-12-01 03:19:25
LyzandeR

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!