ggpubr icon indicating copy to clipboard operation
ggpubr copied to clipboard

Lollipop chart with repeated elements in different groups

Open mirand863 opened this issue 3 years ago • 0 comments

I am trying to plot a lollipop chart with 5 groups and repeated elements in those groups. If all elements have different names it works as expected:

Screenshot from 2022-02-03 14-05-55

The problem is that I want to plot only 5 algorithms in different groups, and when I actually name them from Algorithm 1-5 this happens with the plot:

Screenshot from 2022-02-03 14-09-36

This is my snippet that produces the correct behavior of the lollipop chart (except for the wrong labels):

library(ggpubr)

# Create dataset
data <- data.frame(
  algorithm=paste( "Algorithm ", seq(1,25), sep=""),
  category=as.factor(c( rep('A', 5), rep('B', 5), rep('C', 5), rep('D', 5), rep('E', 5))),
  metric=c(rep(rev(96:100), 5))
)

ggdotchart(data, x = "algorithm", y = "metric",
           color = "category",                                # Color by groups
           palette = c("#264653", "#2a9d8f", "#e9c46a", "#f4a261", "#e76f51"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           rotate = TRUE,                                # Rotate vertically
           group = "category",                                # Order by groups
           dot.size = 7,                                 # Large dot size
           label = round(data$metric),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 8,
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
) +
  labs(y = "Metric (%)", color="")

This is the new data snippet that causes this behavior:

# Create dataset
data <- data.frame(
  algorithm=rep(paste( "Algorithm ", seq(1,5), sep=""), 5),
  category=as.factor(c( rep('A', 5), rep('B', 5), rep('C', 5), rep('D', 5), rep('E', 5))),
  metric=c(rep(rev(96:100), 5))
)

How can I possibly solve this issue?

mirand863 avatar Feb 03 '22 13:02 mirand863