ObservableJS in Quarto

Recreating ggplot plots using OJS

Author

Roy Francis

Published

06-Sep-2024

This is recreated from here.

R
library(ggplot2)
head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Read data

Convert R object to OJS object.

R
ojs_define(ojs_mtcars = mtcars)

Transpose JS object. Preview of JSON object.

OJS
mtcars = transpose(ojs_mtcars)
mtcars

Table

viewof tbl_mtcars = Inputs.table(mtcars)

Scatterplot

R
ggplot(mtcars, aes(wt, mpg, col = cyl)) + 
  geom_point()

OJS
plot_data = Plot.plot({
  color: { legend: true},
  marks: [
    Plot.dot(mtcars, {
      x: "wt",
      y: "mpg",
      fill: "cyl",
      tip: true,
      channels: { Name: "_row" }
    })
  ],
  grid: true
})

Barplot

R
ggplot(mpg, aes(class)) + 
  geom_bar()

R
ojs_define(ojs_mpg = mpg)
OJS
mpg = transpose(ojs_mpg)
OJS
Plot.plot({
  marks: [
    Plot.auto(mpg, { x: "class", y: { reduce: "count" }, mark: "bar" })
  ],
  y: {
    grid: true,
  }
})

With categorical colors

R
ggplot(mpg, aes(class)) + 
  geom_bar(aes(fill = drv))

OJS
Plot.plot({
  color: {
    legend: true
  },
  y: {
    grid: true,
  },
  marks: [
    Plot.auto(mpg, { x: "class", y: { reduce: "count" }, color: "drv", mark: "bar" })
  ]
})

Boxplot

R
ggplot(mpg, aes(class, hwy)) + 
  geom_boxplot()

OJS
Plot.plot({
  marks: [
    Plot.boxY(
      mpg,
      { x: "class", y: "hwy" }
    )
  ]
})

Regression

R
ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  geom_smooth(method="lm")
`geom_smooth()` using formula = 'y ~ x'

OJS
Plot.plot({
  marks: [
    Plot.dot(
      mpg,
      { x: "displ", y: "hwy" }
    ),
    Plot.linearRegressionY(
      mpg, {
      x: "displ",
      y: "hwy",
      stroke: "red",
    }),
  ],
})

Tile/Raster

R
ggplot(faithfuld, aes(waiting, eruptions)) + 
  geom_tile(aes(fill = density))

R
ojs_define(ojs_faithfuld = faithfuld)
OJS
faithfuld = transpose(ojs_faithfuld)
OJS
Plot.plot({
  marks: [
    Plot.raster(
      faithfuld,
      { x: "waiting", y: "eruptions", fill: "density", interpolate: "nearest" }
    )
  ]
})

2D density

R
ggplot(faithfuld, aes(waiting, eruptions)) + 
  geom_density_2d()

OJS
Plot.plot({
  marks: [
    Plot.density(
      faithfuld,
      { x: "eruptions", y: "waiting"  }
    ),
    Plot.dot(
      faithfuld,
      { x: "eruptions", y: "waiting" }
    ),
  ],
  marginLeft: 50,
  marginBottom: 50
})

Hex

R
ggplot(diamonds, aes(carat, price)) + 
  geom_hex()

R
ojs_define(ojs_diamonds = diamonds)
OJS
diamonds = transpose(ojs_diamonds)
OJS
Plot.plot({
  marks: [
    Plot.hexgrid(),
    Plot.dot(
      diamonds,
      Plot.hexbin(
        { r: "count" },
        { x: "carat", y: "price", fill: "currentColor" }
      )
    ),
  ],
  marginLeft: 50,
  marginBottom: 50
})

Line

R
ggplot(economics, aes(date, unemploy)) + 
  geom_line()

R
ojs_define(ojs_economics = economics)
OJS
economics = transpose(ojs_economics)
OJS
Plot.plot({
  marks: [
    Plot.line(
      economics,
      { x: "date", y: "unemploy" }
    )
  ],
  grid: true
})

With categorical colors

R
ggplot(economics_long, aes(date, value01, colour = variable)) + 
  geom_line()

OJS
Plot.plot({
  color: {
    legend: true
  },
  marks: [
    Plot.line(
      economics,
      { x: "date", y: "value01", stroke: "variable" }
    )
  ],
  grid: true
})

Path

R
ggplot(economics, aes(unemploy/pop, psavert)) + 
  geom_path()

OJS
Plot.plot({
  marks: [
    Plot.line(economics, {
      x: (d) => d.unemploy / d.pop,
      y: "psavert",
      z: null,
      stroke: (d) => d.date
    })
  ],
  grid: true,
})