knitr::opts_chunk$set(echo = TRUE, size = "small")
require(mosaic)
theme_set(theme_bw())

This R Markdown document is made interactive using Shiny. Unlike the more traditional workflow of creating static reports, you can now create documents that allow your readers to change the assumptions underlying your analysis and see the results immediately.

To learn more, see Interactive Documents

Inputs and Outputs

You can embed Shiny inputs and outputs in your document. Outputs are automatically updated whenever inputs change. This demonstrates how a plot can be made interactive by wrapping it in the Shiny renderPlot function. The selectInput(), sliderInput(), and checkboxGroupInput() functions create the input widgets used to drive the plot.

The example below illustrates the relationships among histograms, frequency polygons, ASH plots (Average Shifted Histogram), and density plots.

inputPanel(
  selectInput("binwidth", label = "bin width:",
              choices = c(0.1, 0.2, 0.25, 0.5, 1.0, 1.5), selected = 0.5),
  sliderInput("center", label = "center of bin:",
              min = -1.0, max = 1.0, value = 0.0, step = 0.02),
  checkboxGroupInput("show", "Show the following",
                     choices = c(`histogram` = "hist",
                                 `frequency polygon` = "freq",
                                 `ASH plot` = "ash",
                                 `density plot` = "dens"),
                     selected = "hist")
)

renderPlot(
  height = 400,
  {
    # bin_width <- as.numeric(input$binwidth)
    # center <- as.numeric(input$center)


    the_plot <- gf_empty()

    if ("hist" %in% input$show)
      the_plot <- the_plot %>% 
        gf_dhistogram( ~ eruptions, data = faithful, 
                       binwidth = as.numeric(input$binwidth), 
                       center = input$center,
                       fill = "skyblue",
                       color = "gray50",
                       alpha = 0.2, size = 1)
    if ("ash" %in% input$show)
      the_plot <- the_plot %>%
        gf_ash( ~ eruptions, data = faithful,
                binwidth= as.numeric(input$binwidth),
                color = ~"ASH", size = 1.2)
    if ("freq" %in% input$show)
      the_plot <- the_plot %>%
        gf_freqpoly( after_stat(density) ~ eruptions, data = faithful, 
                    binwidth = as.numeric(input$binwidth), 
                    size = 1.2, color = ~"frequency polygon",
                    alpha = 0.7,
                    center = input$center)
    if ("dens" %in% input$show)
      the_plot <- the_plot %>%
        gf_dens( ~ eruptions, data = faithful, adjust = as.numeric(input$binwidth), 
                 color = ~"density", size = 1.2, alpha = 0.7)

    the_plot <- the_plot %>%
      gf_labs(x = "Duration of eruption (minutes)",
              y = "",
              title = "Old Faithful Eruptions",
              color = "Type of plot              ") %>%
      gf_lims(x = c(0.5, 6.5), y = c(0, 1.2)) %>%
      gf_refine(
        scale_color_manual(
          values = c("ASH" = "red", "frequency polygon" = "navy", "density" = "forestgreen",
                     "histogram" = "gray50"))
        )
    the_plot
  })


Try the mosaic package in your browser

Any scripts or data that you put into this service are public.

mosaic documentation built on Nov. 10, 2023, 1:11 a.m.