Plot data with vega-lite from R with the convenience of htmlwidgets infrastructure.
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | ## Not run:
library(vegaliteR)
spec <- jsonlite::fromJSON(
'
{
"data": {
"values": [
{"x":"A", "y":28}, {"x":"B", "y":55}, {"x":"C", "y":43},
{"x":"G", "y":19}, {"x":"H", "y":87}, {"x":"I", "y":52},
{"x":"D", "y":91}, {"x":"E", "y":81}, {"x":"F", "y":53}
]
},
"marktype": "bar",
"encoding": {
"y": {"type": "quantitative", "field": "y"},
"x": {"type": "ordinal", "field": "x"}
}
}
' ,
simplifyDataFrame = FALSE
)
spec2 <- list(
"data" = list(
"values" = list(
list("a"="A", "b"=28),
list("a"="B", "b"=55),
list("a"="C", "b"=43),
list("a"="D", "b"=91),
list("a"="E", "b"=81),
list("a"="F", "b"=53),
list("a"="G", "b"=19),
list("a"="H", "b"=87),
list("a"="I", "b"=52)
)
),
"marktype" = "bar",
"encoding" = list(
"x" = list("type" = "ordinal","field" = "a"),
"y" = list("type" = "quantitative","field" = "b")
)
)
vegalite(spec)
# render with canvas instead of svg
vegalite(spec,"canvas")
# sizing is unfortunately not easy
# https://github.com/vega/vega-lite/issues/194
spec$config$cell$height = 400
spec$encoding$x$scale$bandWidth = 60
vegalite(spec)
# using an R list instead of JSON
vegalite(spec2)
# use rlist handy list.parse function
# to convert data.frames to list
library(rlist)
library(pipeR)
mtcars %>>%
(
data.frame(
name = rownames(.),
.,
stringsAsFactors = FALSE
)
) %>>%
list.parse %>>%
unname() %>>%
(
vegalite(
list(
data = list(
values = .
),
marktype = "bar",
encoding = list(
"x" = list("field" = "cyl","type" = "O"),
"y" = list("field" = "qsec","type" = "Q",aggregate = "mean"),
"color" = list("field" = "cyl","type" = "O")
)
)
)
)
# http://vega.github.io/vega-editor/?mode=vega-lite&spec=stacked_bar
# using R barley data
#{
# "data": {"url": "data/barley.json"},
# "marktype": "bar",
# "encoding": {
# "x": {"name": "yield","type": "Q","aggregate": "sum"},
# "y": {"name": "variety","type": "N"},
# "color": {"name": "site","type": "N"}
# }
#}
data(barley, package = "lattice")
barley %>>%
list.parse %>>%
unname %>>%
(
list(
data = list( values = . ),
marktype = "bar",
encoding = list(
x = list(field = "yield", type = "Q", aggregate = "sum"),
y = list(field = "variety", type = "nominal"), # can use N instead of nominal
color = list(field = "site", type = "nominal")
)
)
) %>>%
vegalite(width = 500)
barley %>>%
list.parse %>>%
unname %>>%
(
list(
data = list( values = . ),
marktype = "bar",
encoding = list(
x = list(field = "yield", type = "Q", aggregate = "sum"),
y = list(field = "site", type = "nominal"),
color = list(field = "site", type = "nominal")
)
)
) %>>%
vegalite(width = 500)
#try with the json
barley2 <- jsonlite::fromJSON("http://vega.github.io/vega-editor/app/data/barley.json",simplifyDataFrame = FALSE)
barley2 %>>%
(
list(
data = list( values = . ),
marktype = "bar",
encoding = list(
x = list(field = "yield", type = "Q", aggregate = "sum"),
y = list(field = "variety", type = "nominal"),
color = list(field = "site", type = "nominal")
)
)
) %>>%
vegalite(width = 500)
data(Oats, package = "MEMSS")
Oats %>>%
list.parse %>>%
unname %>>%
(
list(
data = list(values = .),
marktype = "bar",
encoding = list(
x = list(
field = "yield",
type = "Q",
aggregate = "sum"
),
y = list(
field = "Variety",
type = "nominal"
),
color = list(
field = "Block",
type = "nominal"
)
)
)
) %>>%
vegalite(width = 400)
library(purrr)
diamonds %>>%
(.[sample(1:nrow(.),5000),]) %>>%
by_row(as.list) %>>%
(.$.out) %>>%
(
list(
data = list(values = .),
marktype = "line",
encoding = list(
y = list(field = "price", type = "Q", aggregate = "mean"),
x = list(field = "cut", type = "O"),
color = list(field = "clarity", type = "nominal")
)
)
) %>>%
vegalite(renderer = "canvas")
library(V8)
ctx <- new_context(global = "module")
ctx$source("https://rawgit.com/vega/vega-lite/master/gallery/examples.js")
specs <- ctx$get("EXAMPLES",simplifyDataFrame = FALSE)
vegalite(specs[[1]]$spec)
# if data not in json file could do this
library(htmltools)
browsable(tagList(
lapply(specs,vegalite)
))
# can get json data this way
# manually
specs[[3]]$spec$data <- list(
values = jsonlite::fromJSON(
paste0("http://vega.github.io/vega-editor/app/",specs[[3]]$spec$data$url),
simplifyDataFrame = FALSE
)
)
vegalite(specs[[3]]$spec)
# across all with json data
specs_local <- lapply(
specs,
function(spec){
if("url" %in% names(spec$spec$data)){
spec$spec$data <- list(
values = jsonlite::fromJSON(
paste0(
"http://vega.github.io/vega-editor/app/",
spec$spec$data$url
),
simplifyDataFrame = FALSE
)
)
}
spec
}
)
browsable(tagList(
lapply(specs_local,function(spec){vegalite(spec$spec)})
))
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.