R + ggplot : Time series with events

前端 未结 3 624
梦如初夏
梦如初夏 2020-12-22 15:26

I\'m an R/ggplot newbie. I would like to create a geom_line plot of a continuous variable time series and then add a layer composed of events. The continuous variable and it

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-22 15:58

    Now I like ggplot as much as the next guy, but if you want to make the Google Finance type charts, why not just do it with the Google graphics API?!? You're going to love this:

    install.packages("googleVis")
    library(googleVis)
    
    dates <- seq(as.Date("2011/1/1"), as.Date("2011/12/31"), "days")
    happiness <- rnorm(365)^ 2
    happiness[333:365] <- happiness[333:365]  * 3 + 20
    Title <- NA
    Annotation <- NA
    df <- data.frame(dates, happiness, Title, Annotation)
    df$Title[333] <- "Discovers Google Viz"
    df$Annotation[333] <- "Google Viz API interface by Markus Gesmann causes acute increases in happiness."
    
    ### Everything above here is just for making up data ### 
    ## from here down is the actual graphics bits        ###
    AnnoTimeLine  <- gvisAnnotatedTimeLine(df, datevar="dates",
                                           numvar="happiness", 
                                           titlevar="Title", annotationvar="Annotation",
                                           options=list(displayAnnotations=TRUE,
                                                        legendPosition='newRow',
                                                        width=600, height=300)
                                           )
    # Display chart
    plot(AnnoTimeLine) 
    # Create Google Gadget
    cat(createGoogleGadget(AnnoTimeLine), file="annotimeline.xml")
    

    and it produces this fantastic chart:

    enter image description here

提交回复
热议问题