Diverging Lollipop Chart: A Visual Tool for Comparing Data with {healthyR}

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

A diverging lollipop chart is a useful tool for comparing data that falls into two categories, usually indicated by different colors. This type of chart is particularly well-suited for comparing the differences between two data sets and for identifying which data points are contributing most to the differences.

The R package {healthyR} offers a function called diverging_lollipop_plt() that can be used to create a diverging lollipop chart. This function has several parameters that can be used to customize the chart to meet your specific needs.

In conclusion, the diverging lollipop chart is a useful tool for comparing data sets and can provide insights into the differences between two sets of data. The diverging_lollipop_plt() function from the {healthyR} package is a great option for creating this type of chart, as it offers a range of customization options to meet your specific needs. Whether you’re working with data related to business, finance, or any other field, a diverging lollipop chart can be a valuable tool in your visual analysis toolkit.

Function

Let’s take a look at the full function call.

diverging_lollipop_plt(
  .data,
  .x_axis,
  .y_axis,
  .plot_title = NULL,
  .plot_subtitle = NULL,
  .plot_caption = NULL,
  .interactive = FALSE
)

Now lets see the arguments that get provided to the parameters.

  • .data – The data to pass to the function, must be a tibble/data.frame.
  • .x_axis – The data that is passed to the x-axis. This will also be the x and xend parameters of the geom_segment
  • .y_axis – The data that is passed to the y-axis. This will also equal the parameters of yend and label
  • .plot_title – Default is NULL
  • .plot_subtitle – Default is NULL
  • .plot_caption – Default is NULL
  • .interactive – Default is FALSE. TRUE returns a plotly plot

Example

Let’s see an example.

library(healthyR)

suppressPackageStartupMessages(library(ggplot2))

data("mtcars")
mtcars$car_name <- rownames(mtcars)
mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2)
mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above")
mtcars <- mtcars[order(mtcars$mpg_z), ]  # sort
mtcars$car_name <- factor(mtcars$car_name, levels = mtcars$car_name)

diverging_lollipop_plt(
  .data = mtcars, 
  .x_axis = car_name,
  .y_axis = mpg_z
)

Now let’s also see the interactive chart.

diverging_lollipop_plt(
  .data = mtcars, 
  .x_axis = car_name,
  .y_axis = mpg_z,
  .interactive = TRUE
)

Voila!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)