Legends and labeling of smooth lines + additional lines using ggplot2
I am working on rendering some patterns in network data and have some string marking issues where I have multiple string classes:
- loess lines for each factor (network)
- baseline at y = 4000
- gamma string that acts on all data (ignored)
Now Stack Overflow helped me get to that point (thanks!), But I feel like I'm hitting a brick wall, what I need to do is:
and. provide a legend entry for line # 3
B. mark each line on the graph (according to # 1 # 2 # 3 - so there are only 8 lines)
Here is the code I have so far:
p <- ggplot(network_data, aes(x=timeofday,y=dspeed, colour=factor(network)))+stat_smooth(method="loess",formula=y~x,se=FALSE)
p <- p + stat_function(fun=function(x)4000, geom="line", linetype="dashed", aes(colour="Baseline"))
p <- p + xlab("Time of Day (hr)") + ylab("Download Speed (ms)")
p <- p + theme(axis.line=element_line(colour="black"))
# add the gam line, colouring it purple for now
q <- layer(data=network_data, mapping=aes(x=timeofday,y=dspeed), stat="smooth"
, stat_params=list(method="gam", formula=y~s(x), se=FALSE), geom="smooth", geom_params=list(colour="purple"), position=position_identity())
graph <- p+q # add the layer
#legend
graph <- graph+scale_colour_discrete(name="network")
# set up the origin correctly and axes etc
graph2 <- graph + scale_y_continuous(limits=c(0,6500), expand=c(0,0), breaks=c(0,1000,2000,3000,4000,5000,6000)) + scale_x_datetime(limits=as.POSIXct(c("2015-04-13 00:00:01","2015-04-13 23:59:59")), expand = c(0, 0), breaks=date_breaks("1 hour"), labels=date_format("%H"))
I'd love to look at other packages, but ggplot2 seems to be the best so far.
Is there a way to do this "automatically" (via programming) as I am trying to automate the generation of these graphs?
I made the data available here as a .Rda file:
https://dl.dropboxusercontent.com/u/5268020/network_data.Rda
And here is a picture of the current plot:
source to share
For q B try annotate
and enter the code manually in the box and text to label each line. Seems unnecessary given the legend.
http://docs.ggplot2.org/current/annotate.html
source to share