在ggplot中添加点到堆栈线图

时间:2015-07-03 03:42:14

标签: r ggplot2

我对数据可视化有一个快速的问题。我需要在数据点上创建带有点(geom_point)的堆叠折线图。我可以在下面的代码的帮助下创建堆积图表,但我很难搞清楚如何为数据添加点。

这是test.csv的内容:

Date    Category    Value
3/6/15      A       6.00
3/13/15     A       16.00
3/20/15     A       10.00
3/27/15     A       15.00
4/3/15      A       18.00
4/10/15     A       30.00
3/6/15      B       2
3/13/15     B       5.00
3/20/15     B       12.00
3/27/15     B       17.00
4/3/15      B       19.00
4/10/15     B       29.00
3/6/15      C       10
3/13/15     C       10
3/20/15     C       10
3/27/15     C       10
4/3/15      C       10
4/10/15     C       10

这是我的代码:

df = read.csv("test.csv", header = T)
df$Date = as.Date(df$Date, format = "%m/%d/%y")
ggplot(df, aes(x = Date, y = Value, fill = Category)) + geom_area(colour="black", size=0.2, alpha=.4)

enter image description here

我尝试添加geom_point(),但它会这样做。

enter image description here

我希望这些点在叠加的情节上。任何帮助将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:4)

使用position_stack

ggplot(df, aes(x = Date, y = Value, fill = Category)) + 
  geom_area(colour="black", size=0.2, alpha=.4) +
  geom_point(position=position_stack())

enter image description here