Re: adding a trend line to ggplot2

1,888 views
Skip to first unread message

gsaray101

unread,
Jun 26, 2012, 4:49:32 PM6/26/12
to ggp...@googlegroups.com

On Monday, June 25, 2012 11:09:11 AM UTC-4, gsaray101 wrote:
I am working with a data frame like this:
 
 
df is this
Date       Variable  Value
1/1/2001   a            10
1/1/2001   b             100
1/1/2001   c              50
1/2/2001   a              15
1/2/2001  b                90
1/2/2001  c                60
etc
 
 
I like to draw a stack bar chart which I can with this:
 
ggplot(df, aes(x = Date, y = Value, fill= Variable)) + geom_bar(stat = "identity", position = "stack")+theme_bw()
 
I like to be able to add a trend to this stacked bar chart based on the total values by date. For example, total data fram would look like this:
 
total
Date   Value
1/1/2001  160
1/2/2001   175
etc
 
How can I a trend line using ggplot? Thanks for your help.
 
I could not find a good example on stakcoverflow. I can create the stack bar chart but I would like to add a an arrow line that will show the trend. Any ideas, anybody?
 
 
 

adam.l...@pnc.com

unread,
Jun 26, 2012, 5:41:08 PM6/26/12
to gsaray101, ggp...@googlegroups.com
Hi, try this.

library(grid)
library(ggplot2)

df <- structure(list(Date = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("1/1/2001",
"1/2/2001"), class = "factor"), Variable = structure(c(1L, 2L,
3L, 1L, 2L, 3L), .Label = c("a", "b", "c"), class = "factor"),
    Value = c(10L, 100L, 50L, 15L, 90L, 60L)), .Names = c("Date",
"Variable", "Value"), class = "data.frame", row.names = c(NA,
-6L))

tot <- structure(list(Date = structure(1:2, .Label = c("1/1/2001", "1/2/2001"
), class = "factor"), Value = c(160L, 165L)), .Names = c("Date",
"Value"), class = "data.frame", row.names = c(NA, -2L))

ggplot(df, aes(x = Date, y = Value)) +

geom_bar(aes(fill=Variable),stat = "identity", position = "stack") +

theme_bw()+

geom_line(data=tot,aes(Date,Value,group=1),arrow=arrow())


Adam


From: gsaray101 <gsara...@gmail.com>
To: ggp...@googlegroups.com
Date: 06/26/2012 04:49 PM
Subject: Re: adding a trend line to ggplot2
Sent by: ggp...@googlegroups.com


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example:
https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options:
http://groups.google.com/group/ggplot2


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.


gsaray101

unread,
Jun 27, 2012, 9:49:44 AM6/27/12
to ggp...@googlegroups.com
this is not what I am looking for. This draws curvy line on top of the stack bar. I am looking for a trend line (straigt, up or vertical or down: depending on the trend).

gsaray101

unread,
Jun 27, 2012, 10:10:25 AM6/27/12
to ggp...@googlegroups.com
Adam:
 
I figured it out. I wanted to put the variables in stack and put a trend line on the total value to show where the trend is going. I can do this by this: geom_smooth(data=total,aes(Date,value,group=1), method="lm", size=2, color="darkblue")
 
I have another question. on the same graph. I would like to show the projected line for 6 months. Have you done anything like that?
Reply all
Reply to author
Forward
0 new messages