g2r comes with a number of figures that can be used to create visualisations. However, these can be used in many different ways than what is presented here in order to produce completely different looking charts; see examples, and methods.
See the graph vignette for network-related figures.
The shape of the point can also be set to:
circlebowtiecrossdiamondhexagonhollow-bowtiehollow-circle (default)hollow-diamondhollow-hexagonhollow-trianglehollow-triangle-downlinehyphenplusticksquareThe shape of the interval can also be set to:
rect (default)funnelhollow-rectlinepyramidticktriangle
df <- data.frame(
x = seq.Date(Sys.Date() - 29, Sys.Date(), by = "days"),
y = rnorm(30, mean = rep(c(70,60),each=15), sd = 3)
)
g2(df, asp(x, y)) %>%
fig_interval()
df <- expand.grid(x = letters, y = letters)
df$value <- rnorm(nrow(df))
g2(df, asp(x, y, color = value)) %>%
fig_polygon() %>%
gauge_color_blue()
g2(iris, asp(Sepal.Length, Sepal.Width, color = Petal.Length)) %>%
fig_heatmap() %>%
gauge_color(c("blue", "cyan", "lime", "yellow", "red"))Use count (internally computed) to define the color (optional).
g2(cars, asp(speed, dist, color = count)) %>%
fig_bin() %>%
gauge_color_blue()Use size_count to size the bins by count.
data(diamonds, package = "ggplot2")
g2(diamonds, asp(table, price, color = count)) %>%
fig_bin(
type = "hex",
size_count = FALSE,
stroke = "#fff"
) %>%
gauge_color_blue()
df <- data.frame(
grp = rep(c("A", "B"), each = 200),
val = c(
rnorm(200, mean = 57, sd = 5),
rnorm(200, mean = 53, sd = 5)
)
)
g2(df, asp(val, color = grp)) %>%
fig_histogram(bin_width = 1, fillOpacity = .5)
g2(iris, asp(Sepal.Length, color = Species)) %>%
fig_density()
df <- tidyr::pivot_longer(iris, -Species)
g2(df, asp(name, value, color = Species)) %>%
fig_boxplot(adjust("dodge"))
g2(cars, asp(speed, dist)) %>%
fig_point() %>%
fig_smooth(asp(shape = "smooth"), method = "polynomial")Two types: interval and area.
df <- data.frame(
name = letters[1:5],
value = runif(5)
)
g2(df, asp(y = value, color = name, label = name)) %>%
fig_pie()
df <- dplyr::tibble(
x = runif(50, 1, 500),
y = runif(50, 1, 500),
value = runif(50, 1, 500)
)
g2(df, asp(x, y, color = value)) %>%
fig_voronoi() %>%
gauge_x_linear(nice = FALSE) %>%
gauge_y_linear(nice = FALSE) %>%
gauge_color_blue()
wallgreens <- tidyquant::tq_get("WBA", from = Sys.Date() - 90)
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
#> Registered S3 method overwritten by 'ggplot2':
#> method from
#> print.element g2r
cb <- htmlwidgets::JS(
"(trend) => {
if(trend == 'Up')
return 'green';
return 'red';
}"
)
g2(wallgreens, asp(date, open = open, close = close, high = high, low = low)) %>%
fig_candle() %>%
gauge_x_time_cat() %>%
gauge_color(cb)
df <- data.frame(
x = as.factor(c(1:10, 1:10)),
y = runif(20, 15, 25),
grp = rep(c("A", "B"), each = 2)
)
df$ymin <- df$y - runif(20, 1, 3)
df$ymax <- df$y + runif(20, 1, 3)
g2(df, asp(x = x, color = grp)) %>%
fig_error(
asp(ymin = ymin, ymax = ymax, size = 10),
adjust("dodge")
) %>%
fig_interval(
asp(y = y),
adjust("dodge"),
fillOpacity = .4
)
data(faithfuld, package = "ggplot2")
g2(faithfuld, asp(waiting, eruptions, z = density)) %>%
fig_contour(
binwidth = 0.001,
colors = c("#440154FF", "#21908CFF", "#FDE725FF")
)
g2(faithfuld, asp(eruptions, waiting, z = density)) %>%
fig_contour(
type = "filled",
colors = c("#000004FF", "#B63679FF", "#FCFDBFFF")
) %>%
gauge_x_linear(nice = FALSE) %>%
gauge_y_linear(nice = FALSE)
seg <- df <- data.frame(
x = c(24, 23),
y = c(70, 54),
xend = c(17, 24),
yend = c(50, 93)
)
g2(cars, asp(speed, dist)) %>%
fig_point() %>%
fig_segment(
asp(x = x, y = y, xend = xend, yend = yend),
data = seg
)