tidyverse / tidyverse/ggplot2

position_dodge() with order aesthetics for stacking and dodging

Open
#6,775 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

positions :1st_place_medal:
Dominant language
R
Stars
7k
Forks
2.1k
Avg merge
59m
Merged PRs (30d)
2

Description

This plots as expected:

ggplot(mpg, aes(x = class, order = drv, fill = drv)) +
    geom_bar(position = position_dodge(preserve = "single")) +
    theme_bw()
Image

This one is almost correct, but it seems the widths of the bars take into account the number of groups (5) rather then the number of distinct order values (3):

ggplot(mutate(mpg, drvXyear = paste0(drv, "|", year)),
       aes(x = class, order = drv, fill = drvXyear)) +
    geom_bar(position = position_dodge(preserve = "single")) +
    theme_bw()
Image

ggplot2 v4.0.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two R examples with ggplot2 4.0.1, focusing on the position_dodge() entry point and its preserve = "single" behavior. Trace how order and fill groups determine bar widths, then verify that the second plot uses the three distinct order values rather than the five fill groups.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.