Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dumbell plot with more than one start and end point

I have the following dumbbell plot which has more than one start and end point for certain groups:

library(tidyverse)
df <- structure(list(company = c("A", "A", "A", "A", "B", "B", "C", 
                           "C", "D", "D", "E", "E", "F", "F", "G", "G", "H", "H", "I", "I", 
                           "J", "J", "J", "J"), variable = c("customers", "potential_customers", 
                                                             "customers", "potential_customers", "customers", "potential_customers", 
                                                             "customers", "potential_customers", "customers", "potential_customers", 
                                                             "customers", "potential_customers", "customers", "potential_customers", 
                                                             "customers", "potential_customers", "customers", "potential_customers", 
                                                             "customers", "potential_customers", "customers", "potential_customers", 
                                                             "customers", "potential_customers"), date = structure(c(19840, 
                                                                                                                     19957, 20023, 20223, 19811, 20149, 19773, 20202, 19806, 20135, 
                                                                                                                     19859, 20201, 19815, 19951, 19786, 20085, 19854, 19909, 19831, 
                                                                                                                     19977, 19823, 20089, 20123, 20223), class = "Date")), row.names = c(NA, 
                                                                                                                                                                                         -24L), class = "data.frame")
df
ggplot(df, aes(x = date, y = company)) +
  geom_line() +
  geom_point(aes(color = variable), size = 3) +
  theme(legend.position = "bottom")

enter image description here

What is the simplest approach to add the gaps for A and J in an automated way in this instance? thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add a column to your data which groups the pairs of observations on customers and potential_customers. This column could then be mapped on the group aes, something like:

library(tidyverse)
df |>
  dplyr::mutate(
    group = row_number(),
    .by = c(company, variable)
  ) |>
  ggplot(aes(
    x = date, y = company,
    group = interaction(company, group)
  )) +
  geom_line() +
  geom_point(aes(color = variable), size = 3) +
  theme(legend.position = "bottom")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading