Quarto HTML themes gallery

Quarto comes with several HTML themes built in. They are documented, but coming from the Bootswatch project, the existing documentation doesn’t give a good impression of what each theme looks like when used for an article.

Click images for a larger version.

Plot and boxplot in R without borders

R can be pretty counter-intuitive at times, usually for historical reasons. Here’s one I’ve forgotten several times: drawing a plot and a boxplot without the border/box around it. The default (top row) is the plot with a border/box around it.

For the plot() function, we need the argument bty=”n”, that’s the box type “n” (for none, I guess). For the boxplot, we need frame=FALSE, which behind the scenes sets the same graphical parameter. Oddly enough, frame=FALSE is not mentioned in the help on the boxplot() function.

The precise code for these plots: a bunch of values in myvalues, some random values in xrandom to spread out the points, and then this:

par(mfrow=c(2,2)) # 2 x 2 plots
plot(myvalues ~ xrandom, xlim=c(0.5, 1.5), xlab="", ylab="")
boxplot(myvalues, xlim=c(0.5, 1.5), xlab="", ylab="")
plot(myvalues ~ xrandom, xlim=c(0.5, 1.5), xlab="", ylab="", bty="n")
boxplot(myvalues, xlim=c(0.5, 1.5), xlab="", ylab="", frame=FALSE)

Data Analysis for Social Science: A Friendly and Practical Introduction

Looking to get started with data science, but scared it’d be too complicated? There’s a new book by Elena Llaudet and Kosuke Imai that will get you covered. Data Analysis for Social Science: A Friendly and Practical Introduction is now available as an e-book, and it truly delivers what the title claims: friendly and practical. It’s also up-to-date, with a focus on experimental data and causal inference much more than on multiple regression analysis. I don’t think I’ve seen a more accessible introduction to R and Rstudio — cheat-sheets included!

How to specify RGB colours and transparency in R plots

R handles colours like “red” or “blue” out of the box, but what if we want more precise colours? Enter rgb():

Here’s some simple code to illustrate how to use rgb in R: Let’s just draw 10 circles in pure red (red=1=100%, green=0%, blue=0%).

plot(1:10, rep(1,10), col=rgb(1, 0, 0, 0.5), pch=16)

The additional number here (0.5) is the alpha value to indicate transparency. 0.5 is 50% transparent.

Let’s add blue points:
points((1:10)+0.05, rep(1,10), col=rgb(0, 0, 1, 0.5), pch=16)

For intermediary colours, we need to specify the R, G, and B values as fractions: the division by 255 is not typically included on colour palettes.

rgb(68/255, 119/255, 170/255, 0.5)

Combining two strings in R when they are in a vector

Joining two strings in R can readily be done with the paste() command. However, if our strings are part of a vector, paste() no longer works as we might expect.

To illustrate this, assume a list of characters or symbols, and a set of values you want to convert into these characters. The characters “A” to “F” may be more transparent for the example, but it was the bars that led me to this problem: _ ▁ ▂ ▃ ▅ ▇. I use the Unicode values of these bars.

characters = c("A", "B", "C", "D", "E", "F")
characters = c("_", "\u2581", "\u2582", "\u2583", "\u2585", "\u2587")

Let’s start with a couple of random values:

values = runif(5)

In my example, I got:

[1] 0.9568333 0.4533342 0.6775706 0.5726334 0.1029247

Then we want to select one of the characters or symbols based on these values, picking the longer bar for longer values — effectively creating our own histogram (sparkline).

selected = characters[round(values * 5)+1]

This works well, except that we have a series of characters rather than a single string with all the characters included:

"▇" "▂" "▃" "▃" "▁"

My next step was paste() or actually the shorthand paste0() which does not use separation, but it’s not really creating a single string:


[1] "▇" "▂" "▃" "▃" "▁"

enter library(stringr) and the str_c() command:

str_c(selected, sep="", collapse="")

[1] "▇▂▃▃▁"

There we go, a single string…