王亮博 (亮亮)
shared under CC 4.0 BY
Esc to overview
← → to navigate
Online slide on http://ccwang002.gitcafe.com/ChinaRConf-Interactive-Vis/
My honor to be the first in this section. This is an introductory talk.
With today's method, we can reveal more details, provide different view points, and can be fancier :)
Web and browsers dominates our front-end world.
Almost every PC and mobile have a modern browser today.
← Hope SVG here someday
We take the first way in this talk.
Full intro can be found on Mozilla Developer Network.
<svg version="1.1" width="300" height="300"
xmlns="http://www.w3.org/2000/svg">
<rect x="40" y="30" width="200" height="200" rx="20"
fill="red" stroke="black" stroke-width="2" />
<rect x="80" y="60" width="200" height="200" rx="40"
fill="blue" stroke="black" stroke-width="2"
fill-opacity="0.7" />
</svg>
But specify each element one by one is hard.
SVG style can be specified by CSS
<svg>
<rect class="myrect" ... />
<rect class="myrect" id="upper" ... />
</svg><style>
.myrect {
fill: red;
stroke: black; stroke-width: 2px;
}
#upper {
fill: blue; fill-opacity: 0.7;
}
</style>
Use CSS3 interaction ability
<style>
.myrect:hover {
fill: white;
stroke: green;
stroke-width: 10;
transition: 0.75s;
}
</style>
So you get an interactive SVG!
Show the concept by a quick demo.
library(grid)
grid.newpage()
pushViewport(plotViewport(c(5, 4, 2, 2)))
pushViewport(dataViewport(
pressure$temperature, pressure$pressure,
name="plotRegion"
))
grid.points(
pressure$temperature, pressure$pressure,
name="dataSymbols"
) # upper figure
grid.rect(gp=gpar(fill=0))
grid.xaxis()
grid.yaxis() # lower figure
grid.edit("dataSymbols", pch=10)
upViewport(1) # inner
grid.rect(gp=gpar(lty="dashed", fill=0))
upViewport(1) # outer
grid.rect(gp=gpar(lty="dotted", fill=0))
# upper plot
downViewport("plotRegion")
grid.text(
"Pressure (mm Hg)\nversus\nTemperature (Celsius)",
just="right",
x=unit(250, "native"), y=unit(600, "native")
)
# lower plot
Using grid to export to SVG is just a filename away.
require("ggplot2")
g <- qplot(clarity, data=diamonds, fill=cut, geom="bar")
ggsave(file="ggplot2_direct.svg",
plot=g, width=10, height=8)
Direct SVG Result
g <- ggplot(...) + ... # plot your ggplot2 here
g.svg <- grid.export("demo.svg", addClasses=TRUE)
(The text is selectable)
Like this approach?
We mentioned two ways about interactive visualization in R.
Package-dependent implementation.
All generates R plots through HTML, SVG, JS, CSS.