Using the plotcli R6 Class

Claas Heuer

2025-11-27

Using plotcli R6 class

Setting up a scatter plot with two data sets

library(plotcli)  

plot_width = 80
plot_height = 40

# Create some example data
x <- seq(0, 2 * pi, length.out = 100)
y1 <- sin(x)
y2 <- cos(x)

data_1 = list(
              x = x, 
              y = y1, 
              name = "y1 = sin(x)", 
              color = "blue",
              type = "line",
              braille = FALSE
              )

data_2 = list(
                x = x, 
                y = y2, 
                name = "y2 = cos(x)", 
                color = "red",
                type = "line",
                braille = FALSE
                )

# Create a plotcli object with specified dimensions
plot <- plotcli$new(
                    plot_width = plot_width, 
                    plot_height = plot_height, 
                    x_label = "X-axis", 
                    y_label = "Y-axis"
                    )

# Add data sets to the plot
plot$add_data(data_1)
plot$add_data(data_2)

# Print the plot
plot$print_plot()
#> 
#>                                                                                                         
#>                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      
#>             1.0 โ”‚******         **********                                                  *****โ”‚      
#>                 โ”‚      *       **         **                                               *     โ”‚      
#>                 โ”‚      **     *             *                                             **     โ”‚      
#>                 โ”‚        *  **               *                                           *       โ”‚      
#>                 โ”‚         * *                 *                                         *        โ”‚      
#>                 โ”‚          **                  *                                      **         โ”‚      
#>                 โ”‚         * *                   *                                     *          โ”‚      
#>                 โ”‚        *   *                   *                                   *           โ”‚      
#>                 โ”‚       *     *                  *                                  *            โ”‚      
#>                 โ”‚      *      *                   *                                *             โ”‚      
#>             0.5 โ”‚      *       *                   *                               *             โ”‚      
#>                 โ”‚     *         *                   *                             *              โ”‚      
#>                 โ”‚     *         *                    *                            *              โ”‚      
#>                 โ”‚    *           *                   *                           *               โ”‚      
#>                 โ”‚   *             *                  *                          *                โ”‚      
#>                 โ”‚  *              *                   *                        *                 โ”‚      
#>                 โ”‚  *               *                   *                       *                 โ”‚      
#>                 โ”‚ *                 *                   *                     *                  โ”‚      
#>                 โ”‚ *                 *                    *                    *                  โ”‚   y1 = sin(x)  
#>   Y-axis    0.0 โ”‚*                   *                   *                   *                  *โ”‚   y2 = cos(x)  
#>                 โ”‚                    *                   *                  *                   *โ”‚      
#>                 โ”‚                     *                   *                 *                  * โ”‚      
#>                 โ”‚                      *                   *               *                   * โ”‚      
#>                 โ”‚                       *                   *             *                    * โ”‚      
#>                 โ”‚                       *                   *             *                   *  โ”‚      
#>                 โ”‚                       *                    *           *                   *   โ”‚      
#>                 โ”‚                        *                   *           *                  *    โ”‚      
#>                 โ”‚                         *                   *         *                  *     โ”‚      
#>                 โ”‚                          *                   *       *                   *     โ”‚      
#>            -0.5 โ”‚                           *                   *     *                    *     โ”‚      
#>                 โ”‚                           *                    *    *                   *      โ”‚      
#>                 โ”‚                            *                   *   *                   *       โ”‚      
#>                 โ”‚                             *                   * *                   *        โ”‚      
#>                 โ”‚                              *                   *                   *         โ”‚      
#>                 โ”‚                               *                 * *                 *          โ”‚      
#>                 โ”‚                                *               *   *               **          โ”‚      
#>                 โ”‚                                 *             *     *             *            โ”‚      
#>                 โ”‚                                  **         **       **         **             โ”‚      
#>                 โ”‚                                    *       *           **     ***              โ”‚      
#>            -1.0 โ”‚                                     *******             ******                 โ”‚      
#>                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      
#>                                                                                                         
#>                   0.0                1.6                3.1                 4.7               6.3       
#>                                                                                                         
#>                                                      X-axis                                             
#> 

Combining Ascii and braille characters

plotcli can use braille characters for plotting scatter and line plots, which give a much higher resolution on supported terminals. Whether to use braille or ascii characters can be specified for each data set.

library(plotcli)  

data(mtcars)

# Fit a linear model
lm_fit <- lm(wt ~ mpg, data = mtcars)

# Create a new dataset with the predicted values
predicted_data <- data.frame(mpg = mtcars$mpg, predicted_wt = predict(lm_fit, mtcars))

# we use braille characters for the regression line
data_1 = list(
              x = predicted_data$mpg, 
              y = predicted_data$predicted_wt, 
              name = "Regression Line", 
              color = "red",
              type = "line",
              braille = TRUE
              )

# and ascii characters for the raw data
data_2 = list(
                x = mtcars$mpg, 
                y = mtcars$wt, 
                name = "Data Points", 
                color = "blue",
                type = "scatter",
                braille = FALSE
                )

# Create a plotcli object
plot_width = 80
plot_height = 40
plot_obj <- plotcli$new(
                        plot_width, 
                        plot_height, 
                        x_label = "Miles per Gallon", 
                        y_label = "Weight"
                        )

# Add raw data and regression line
plot_obj$add_data(data_1)
plot_obj$add_data(data_2)

# Print the plot
plot_obj$print_plot()
#> 
#>                                                                                                         
#>                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      
#>             5.4 โ”‚*              *                                                                โ”‚      
#>                 โ”‚*                                                                               โ”‚      
#>                 โ”‚                                                                                โ”‚      
#>                 โ”‚                                                                                โ”‚      
#>                 โ”‚                                                                                โ”‚      
#>                 โ”‚                                                                                โ”‚      
#>                 โ”‚                                                                                โ”‚      
#>                 โ”‚โก€                                                                               โ”‚      
#>                 โ”‚โ ˆโ ’โข„                                                                             โ”‚      
#>                 โ”‚   โ ‘โ ฆโก€                                                                          โ”‚      
#>             4.4 โ”‚     โ ˆโ ณโฃ„                                                                        โ”‚      
#>                 โ”‚        โ ™โขฆโก€                                                                     โ”‚      
#>                 โ”‚          โ ‰โ ณโฃ„โก€      *                                                           โ”‚      
#>                 โ”‚            โ ˆโ ›โขฆโฃ€                                                                โ”‚      
#>                 โ”‚          *    โ ™โ ณโฃ„            *                                                 โ”‚      
#>                 โ”‚                *โ ˆโ ›โขฆโก€  *                                                        โ”‚      
#>                 โ”‚                    โ ™โ ฒโฃ„                                                         โ”‚      
#>                 โ”‚             *  **    โ ˆโ ‘โขฆโก€                                                      โ”‚      
#>                 โ”‚                *        **โฃ„* *                                                 โ”‚   Regression Line  
#>   Weight    3.3 โ”‚                           โ ˆโ ™โขฆโฃ€                                                 โ”‚   Data Points  
#>                 โ”‚                              โ ‰โ ณโฃ„    *                                          โ”‚      
#>                 โ”‚                  *             โ ˆโ ™โขฆโก€      *     *                               โ”‚      
#>                 โ”‚                                   โ ‰โ ฒโฃ„                                          โ”‚      
#>                 โ”‚                                     โ ˆโ ‘โขฆโก€                                       โ”‚      
#>                 โ”‚                                    **  โ ™โ ฒโฃ„                                     โ”‚      
#>                 โ”‚                                *         โ ˆโ ‘โขฆโก€                                  โ”‚      
#>                 โ”‚                                    *        โ ˆโ ฒโฃ„                                โ”‚      
#>                 โ”‚                                                โ ‘โขฆโก€                             โ”‚      
#>                 โ”‚                                      *           โ ˆโ ฒโฃ„                           โ”‚      
#>             2.3 โ”‚                                          *          โ ‘โขฆโก€                        โ”‚      
#>                 โ”‚                                                       โ ˆโ ฒโฃ„                 *    โ”‚      
#>                 โ”‚                                                     *    โ ‘โขคโก€                   โ”‚      
#>                 โ”‚                                                            โ ‰โ ขโฃ„                 โ”‚      
#>                 โ”‚                                                          *   โ ˆโ ‘โขคโก€              โ”‚      
#>                 โ”‚                                                                 โ ‰โ ขโฃ„           *โ”‚      
#>                 โ”‚                                                                   โ ˆโ ‘โขฆโก€         โ”‚      
#>                 โ”‚                                                                    * โ ‰โ ฒโฃ„       โ”‚      
#>                 โ”‚                                                                    *   โ ˆโ “โขฆโก€    โ”‚      
#>                 โ”‚                                                                           โ ™โ ขโก€  โ”‚      
#>             1.3 โ”‚                                                                             โ ˆโ ’โข„โ”‚      
#>                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      
#>                                                                                                         
#>                  10.4               16.3               22.1                28.0              33.9       
#>                                                                                                         
#>                                                 Miles per Gallon                                        
#>