Change axis limits on googleVis timeline chart
Asked Answered
N

1

6

I would like to set a specific start and end date for an R googleVis timeline chart. For example I would like the following chart to start at 1780 and end at 1815. I have searched the documentation and tried some of the options from other chart types but I have been unable to get any of them to work.

library(googleVis)
datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
                        Name=c("Washington", "Adams", "Jefferson",
                               "Adams", "Jefferson", "Burr"),
                        start=as.Date(x=rep(c("1789-03-29", "1797-02-03", 
                                              "1801-02-03"),2)),
                        end=as.Date(x=rep(c("1797-02-03", "1801-02-03", 
                                            "1809-02-03"),2)))

    Timeline <- gvisTimeline(data=datTL, 
                             rowlabel="Name",
                             barlabel="Position",
                             start="start", 
                             end="end",
                             options=list(timeline="{groupByRowLabel:false}",
                                          backgroundColor='#ffd', 
                                          height=350,
                                          colors="['#cbb69d', '#603913', '#c69c6e']"))
    plot(Timeline)

enter image description here

Neuburger answered 4/4, 2018 at 15:7 Comment(1)
If it is not possible please let me know.Neuburger
R
1

You can supply the minValue and maxValue of horizontal axis via hAxis in options like this:
option=list(hAxis="{minValue: new Date(1785, 0, 0), maxValue: new Date(1810, 0, 0)}"))

The whole plot would be like this:

library(googleVis)
datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
                    Name=c("Washington", "Adams", "Jefferson",
                           "Adams", "Jefferson", "Burr"),
                    start=as.Date(x=rep(c("1789-03-29", "1797-02-03", 
                                          "1801-02-03"),2)),
                    end=as.Date(x=rep(c("1797-02-03", "1801-02-03", 
                                        "1809-02-03"),2)))

option_list <- list(timeline="{groupByRowLabel:false}",
                    backgroundColor='#ffd', 
                    height=350,
                    colors="['#cbb69d', '#603913', '#c69c6e']",
                    hAxis="{minValue: new Date(1785, 0, 0), maxValue: new Date(1810, 0, 0)}")

Timeline <- gvisTimeline(data=datTL, 
                         rowlabel="Name",
                         barlabel="Position",
                         start="start", 
                         end="end",
                         options=option_list)
plot(Timeline)
Rump answered 13/4, 2018 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.