Debian Codegraph @

demo/Roles.R

raw
1 # Add the ability to pass columns roles for further
2 # processing downstream
3 #
4 # Thanks to Oliver Gjoneski
5 #
6 # Role columns must follow column they pertain to. Proper naming
7 # conventions must be be observed. For example, roles fulfilling tooltip
8 # roles and must be called "foo.blah.tooltip".
9 #
10 # For more details see:
11 # https://developers.google.com/chart/interactive/docs/roles
12
13 ## ---- Tooltip ----
14 df <- data.frame(year=1:11,pop=1:11,
15 pop.html.tooltip=letters[1:11])
16 plot(
17 gvisScatterChart(df)
18 )
19
20 ## ---- TooltipHTML ----
21 levels(df$pop.html.tooltip)[1] <-
22 '<a href="https://www.r-project.com"><img src="https://www.r-project.org/Rlogo.jpg" alt="R logo" /></a>'
23 plot(
24 gvisScatterChart(df,
25 options=list(tooltip="{isHtml:'true'}"))
26 )
27
28 ## ---- CertaintyScopeEmphasis ----
29 df <- data.frame(year=1:11, x=1:11,
30 x.scope=c(rep(TRUE, 8), rep(FALSE, 3)),
31 y=11:1, y.html.tooltip=LETTERS[11:1],
32 y.certainty=c(rep(TRUE, 5), rep(FALSE, 6)),
33 y.emphasis=c(rep(FALSE, 4), rep(TRUE, 7)))
34 plot(
35 gvisScatterChart(df,options=list(lineWidth=2))
36 )
37
38 ## ---- ColumnChart ----
39 dat <- data.frame(Year=2010:2013,
40 Sales=c(600, 1500, 800, 1000),
41 Sales.html.tooltip=c('$600K in our first year!',
42 'Sunspot activity made this our best year ever!',
43 '$800K in 2012.',
44 '$1M in sales last year.'),
45 Sales.certainty=c(TRUE, FALSE, TRUE, FALSE))
46 plot(
47 gvisColumnChart(dat, xvar='Year',
48 yvar=c('Sales', 'Sales.certainty')
49 )
50 )
51
52 ## ---- Interval ----
53 df <- data.frame(Year=2013:2014, Sales=c(120, 130),
54 Sales.interval.1=c(100,110),
55 Sales.interval.2=c(140, 150),
56 Sales.interval.3=c(90, 100),
57 Sales.interval.4=c(150, 170),
58 Sales.style=c('red', 'gold'),
59 Sales.annotation=c("North", "South"),
60 check.names=FALSE)
61
62 plot(
63 gvisBarChart(df, xvar='Year',
64 yvar=c('Sales',
65 'Sales.interval.1',
66 'Sales.interval.2',
67 'Sales.style',
68 'Sales.annotation')
69 )
70 )
71
72 plot(
73 gvisLineChart(df, xvar='Year',
74 yvar=c('Sales',
75 'Sales.interval.1',
76 'Sales.interval.2'),
77 options=list(series="[{color:'purple'}]")
78 )
79 )
80
81
82 plot(
83 gvisLineChart(df, xvar='Year',
84 yvar=c('Sales',
85 'Sales.interval.1',
86 'Sales.interval.2',
87 'Sales.interval.3',
88 'Sales.interval.4'),
89 options=list(series="[{color:'purple'}]",
90 lineWidth=4,
91 interval="{
92 'i1': { 'style':'line', 'color':'#D3362D', 'lineWidth': 0.5 },
93 'i2': { 'style':'line', 'color':'#F1CA3A', 'lineWidth': 1 },
94 'i3': { 'style':'line', 'color':'#5F9654', 'lineWidth': 2 },
95 'i4': { 'style':'line', 'color':'#5F9654', 'lineWidth': 3 }
96 }")
97 )
98 )
99
100
101 plot(
102 gvisLineChart(df, xvar='Year',
103 yvar=c('Sales',
104 'Sales.interval.1',
105 'Sales.interval.2',
106 'Sales.interval.3',
107 'Sales.interval.4'),
108 options=list(series="[{color:'purple'}]",
109 lineWidth=4,
110 intervals="{ 'style':'area' }",
111 interval="{
112 'i1': { 'color': '#4374E0', 'style':'bars', 'barWidth':0, 'lineWidth':4, 'pointSize':10, 'fillOpacity':1 },
113 'i2': { 'color': '#E49307', 'style':'bars', 'barWidth':0, 'lineWidth':4, 'pointSize':10, 'fillOpacity':1 }}"
114 )
115 )
116 )
117
118
119 ## ---- TwoLines ----
120 df <- data.frame(country=c("US", "GB", "BR"),
121 val1=c(1,3,4),
122 val1.emphasis=c(TRUE, TRUE, FALSE),
123 val2=c(23,12,32))
124 plot(
125 gvisLineChart(df, xvar="country",
126 yvar=c("val1", "val1.emphasis")
127 )
128 )
129
130 plot(
131 gvisLineChart(df, xvar="country",
132 yvar=c("val1", "val1.emphasis", "val2")
133 )
134 )
135
136 ## ---- VerticalReferenceLine ----
137 dat <- data.frame(Year=2010:2013,
138 Sales=c(600, 1500, 800, 1000),
139 Sales.annotation=c('First year', NA, NA, 'Last Year'),
140 Sales.annotationText=c('$600K in our first year!',
141 NA,
142 NA,
143 '$1M in sales last year.'))
144
145 plot(
146 gvisLineChart(dat, xvar='Year',
147 yvar=c('Sales',
148 'Sales.annotation',
149 'Sales.annotationText'),
150 options=list(annotations = "{style:'line'}")
151 )
152 )
153
154 ## ---- GeoChartTooltip ----
155 Exports$Profit.tooltip <- paste("<b>Test</b>", Exports$Profit)
156 Exports$Tooltip.header <- ""
157 GeoTooltip <- gvisGeoChart(
158 Exports,
159 locationvar = "Country",
160 colorvar = "Profit",
161 hovervar = "Tooltip.header",
162 options = list(tooltip = "{isHtml: true}")
163 )
164 plot(GeoTooltip)