library(palmerpenguins)
library(tidyverse)
ggplot(data = penguins, aes(x = flipper_length_mm)) +
geom_histogram(aes(fill = species),
alpha = 0.5,
position = "identity") +
scale_fill_manual(values = c("darkorange","purple","cyan4")) +
labs(x = "Flipper length (mm)",
y = "Frequency",
title = "Penguin Flipper Lengths",
fill = NULL) +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.9, 0.9))
A common misunderstanding about Quarto is that we cannot use multiple languages within a document. Indeed, Jupyter cannot use multiple languages within a document, and we usually use the jupyter engine for Python and Julia (and it is officially supported.)
However, knitr has already been able to execute multiple languages tons of years ago, so why can’t we do it in Quarto?
Let’s see the following example with palmerpenguins::penguins
of Horst, Hill, and Gorman (2020)
R
Python
To run a python code on knitr, install.packages("reticulate")
from palmerpenguins import load_penguins
import seaborn as sns
import matplotlib.pyplot as plt
= load_penguins()
penguins
plt.clf()=penguins, x='flipper_length_mm',
sns.histplot(data='species', palette=['#FF8C00', '#159090', '#A034F0'])
hue
"Flipper length (mm)")
plt.xlabel("Frequncy")
plt.ylabel(= "")
plt.legend(title plt.show()
Julia
To run a julia code on knitr, run install.packages("JuliaCall")
. PalmerPenguins.load()
asks you to download the data, so you have to set ENV["DATADEPS_ALWAYS_ACCEPT"] = true
to automatically accept it.
using PalmerPenguins
using StatsPlots
using DataFrames
ENV["DATADEPS_ALWAYS_ACCEPT"] = true;
= DataFrame(PalmerPenguins.load());
penguins
histogram(
.== species, :].flipper_length_mm for species in ["Adelie", "Chinstrap", "Gentoo"]],
[penguins[penguins.species = ["Adelie" "Chinstrap" "Gentoo"],
label = [:darkorange :purple :cyan4],
fillcolor = 0.5,
fillalpha = "Flipper length (mm)",
xlabel = "Frequency",
ylabel = "Penguin Flipper Lengths",
title = :topright,
legend = false,
grid = 7,
legendfontsize = nothing,
legendtitle = 5,
bar_width = (480, 320),
size = 300,
dpi = 6,
tickfontsize = 10,
guidefontsize = 12,
titlefontsize =Plots.Measures.Length(:mm, 2.0)
margin )
Stata
Douglas Hemken’s Statamarkdown allows us to use Stata on Rmarkdown (so as on Quarto!)
R
# devtools::install_github("Hemken/Statamarkdown")
library(Statamarkdown)
::write_dta(penguins, here::here("blog/2023/04/30/quarto_multi_lang/palmerpenguins.dta")) haven
Thanks to Statamarkdown
, we can easily set the working directory to the directory where the .qmd
file exists by just calling cd
in Stata.
Stata
cd
C:\Users\kazuh\GitHub\kazuyanagimoto.github.io\blog\2023\04\30\quarto_multi_lan
> g
Then, you can run Stata code in a code chunk.
use palmerpenguins.dta, clear
twoway (hist body_mass_g if species == 1, color(orange%40)) ///
hist body_mass_g if species == 2, color(emerald%40)) ///
(hist body_mass_g if species == 3, color(purple%40)), ///
(xtitle("Body Mass (g)") ytitle("Frequency") ///
legend(order(1 "Adelie" 2 "Chinstrap" 3 "Gentoo") pos(1) ring(0) col(1)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph export "figure/stata/hist.svg", replace
But you have to manually export an image file for a plot and add it into a document…
Tabset
I think, one of the most useful case is when you want to support multiple source codes in a textbook or lecture notes. Quarto supports Tabset layout and it’s so smart!
|>
penguins ggplot(aes(x = flipper_length_mm, y = body_mass_g,
color = species, shape = species)) +
geom_point(size = 3) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
labs(x = "Flipper Length (mm)", y = "Body Mass (g)", color = NULL, shape = NULL) +
theme_minimal() +
theme(legend.position = c(0.9, 0.1),
panel.grid.major = element_blank())
plt.clf()
=penguins, x='flipper_length_mm', y='body_mass_g',
sns.scatterplot(data='species', style="species",
hue=['#FF8C00', '#159090', '#A034F0'])
palette
'Flipper Length (mm)')
plt.xlabel('Body Mass (g)')
plt.ylabel(= "")
plt.legend(title plt.show()
@df penguins scatter(
:flipper_length_mm,
:body_mass_g,
= :species,
group = 5,
markersize = [:circle :utriangle :rect ],
markershape = :white,
markerstrokecolor = ["#FF8C00", "#159090", "#A034F0"],
palette = "Flipper Length (mm)",
xaxis = "Body Mass (g)",
yaxis = (480, 320),
size = 300,
dpi = 8,
legendfontsize = 6,
tickfontsize = 10,
guidefontsize =Plots.Measures.Length(:mm, 2.0)
margin )
use data/palmerpenguins, clear
separate body_mass_g, by(species)
twoway scatter body_mass_g? flipper_length_mm, ///
orange emerald purple) mlab("") ///
mcolor(msymbol(O T S) ///
xtitle("Flipper Length (mm)") ytitle("Body Mass (g)") ///
legend(order(1 "Adelie" 2 "Chinstrap" 3 "Gentoo") pos(11) ring(0) col(1)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
qui graph export "figure/stata/scatter.svg", replace
Have a happy Quarto life 🥂!