Skip to content

coord_flip is inconsistent between ordered geom and legend. #6217

@carljpearson

Description

@carljpearson

I found a problem with coord_flip. The legend is in the opposite order of an ordered geom (like geom_bar).

I expected the legend and order to appear in the same vertical order.

This was previously reported here, but closed: #704

Here is the code to reproduce the bug:

# Create a sample dataset
df1 <- data.frame(
  category = rep(c("A", "B", "C"), each = 3),
  subcategory = rep(c("X", "Y", "Z"), times = 3),
  value = c(10, 20, 30, 15, 25, 35, 20, 30, 40)
)

# Print the dataset
print(df)

# Create a bar graph using ggplot2 with the fill aesthetic
ggplot(df1, aes(x = category, y = value, fill = subcategory)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Bar Graph with Fill Aesthetic",
       x = "Category",
       y = "Value",
       fill = "Subcategory") +
  theme_minimal() +
  coord_flip()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions