R
with flextable
David Gohel's package flextable
is a wonderful addition to the R table-building ecosystem. flextable
is the only solution I know for tables that are not over-padded and way too big.
library(svglite)
library(htmltools)
x <- 1967:1977
y <- c(0.5,1.8,4.6,5.3,5.3,5.7,5.4,5,5.5,6,5)
s <- svgstring(height = 4, width = 6)
plot(y ~ x, axes=F, xlab="", ylab="", pch=16, type="b")
axis(1, at=x, label=x, tick=F, family="serif")
axis(2, at=seq(1,6,1), label=sprintf("$%s", seq(300,400,20)), tick=F, las=2, family="serif")
abline(h=6,lty=2)
abline(h=5,lty=2)
text(max(x), min(y)*2.5,"Per capita\nbudget expanditures\nin constant dollars", adj=1,
family="serif")
text(max(x), max(y)/1.08, labels="5%", family="serif")
dev.off()
# show as standalone
browsable(HTML(s()))
# now add to a flextable
library(flextable)
library(pipeR)
data.frame(
name = as.character(
a(href="http://motioninsocial.com/tufte/#minimal-line-plot-in-base-graphics", "minimal line plot from Tufte in R")
),
chart = HTML(s()),
stringsAsFactors = FALSE
) %>>%
flextable() %>>%
theme_vanilla() %>>%
align(align = "center", part = "all" ) %>>%
width(j = ~chart, width = 10) %>>%
height(height = 3) %>>%
tabwid()
# for fun let's add another row with lattice dot-dash plot
library(lattice)
s2 <- svgstring(height=4, width=6)
x <- mtcars$wt
y <- mtcars$mpg
xyplot(y ~ x, xlab="Car weight (lb/1000)", ylab="Miles per gallon of fuel",
par.settings = list(axis.line = list(col="transparent")),
panel = function(x, y,...) {
panel.xyplot(x, y, col=1, pch=16)
panel.rug(x, y, col=1, x.units = rep("snpc", 2), y.units = rep("snpc", 2), ...)})
dev.off()
data.frame(
name = c(
as.character(
a(href="http://motioninsocial.com/tufte/#minimal-line-plot-in-base-graphics", "minimal line plot from Tufte in R")
),
as.character(
a(href="http://motioninsocial.com/tufte/#dot-dash-plot-in-lattice", "lattice dot-dash plot from Tufte in R")
)
),
chart = c(
HTML(s()),
HTML(s2())
),
stringsAsFactors = FALSE
) %>>%
flextable() %>>%
theme_vanilla() %>>%
align(align = "center", part = "all" ) %>>%
width(j = ~chart, width = 4) %>>%
height(height = 2) %>>%
tabwid()