Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rotation direction set up randomly for pie chart. #6250

Closed
withr opened this issue Dec 18, 2024 · 8 comments
Closed

rotation direction set up randomly for pie chart. #6250

withr opened this issue Dec 18, 2024 · 8 comments

Comments

@withr
Copy link

withr commented Dec 18, 2024

I found a problem with geom_bar

I expected the brown part of the second pie chart should be on the left, but it didn't.

I found it depends on the order of color code. How to fix it?

Here is the code to reproduce the bug:

dat1 <- data.frame(year = c(2012, 2019), 
                   value = c(1, 1), 
                   color = c("#1CF900", "#FFA600"))
dat2 <- data.frame(year = c(2019, 2023), 
                   value = c(1, 1), 
                   color = c("#FFA600", "#FF4000"))

pie1 <- ggplot(dat1, aes(x = "", y = value, fill = color)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar(theta = "y", start = 0) +
  theme_void() +
  theme(legend.position = "none") +
  scale_fill_identity() 

pie2 <- ggplot(dat2, aes(x = "", y = value, fill = color)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar(theta = "y", start = 0) +
  theme_void() +
  theme(legend.position = "none") +
  scale_fill_identity() 

print(pie1 + pie2)

Image

@clauswilke
Copy link
Member

The ordering of categorical variables is determined by the data values (alphabetical by default), and since you're using color codes as data values they are used for ordering.

I would strongly advise against using scale_fill_identity() unless you truly know what you're doing.

Also, this may help: https://wilkelab.org/SDS375/slides/getting-things-in-order.html#1

@withr
Copy link
Author

withr commented Dec 19, 2024

Thanks for your quick reply! But, the data value are 1 for both categories, and I didn't use color codes as data values. the color codes is for color only. Even I changed the value, the output didn't change. Please take a further look of this issue. @clauswilke

@teunbrand
Copy link
Collaborator

I agree with Claus that this is not a bug. These issues here on github are for tracking bugs and feature requests. The issue you've surfaced here is a usage question which would have a better home on stackoverflow or the posit community.

@withr
Copy link
Author

withr commented Dec 19, 2024

@teunbrand Can you provide a script to solve the problem if you take this as a usage question?

@teunbrand
Copy link
Collaborator

This is not the place for that. You've already been directed to more resources by Claus: https://wilkelab.org/SDS375/slides/getting-things-in-order.html#1. If these are insufficient to resolve your problem, I'll repeat that a better venue for usage questions is stackoverflow or the posit community.

@withr
Copy link
Author

withr commented Dec 19, 2024

Sad that none of you take this as bug. If the function can't generate consistent output, it's a bug.

@smouksassi
Copy link

it is unclear what are you trying to achieve and what is expected.

library(ggplot2)
library(patchwork)
dat1 <- data.frame(year = c("2012", "2019"), 
                   value = c(1, 1), 
                   pie = "one")
dat2 <- data.frame(year = c("2019", "2023"), 
                   value = c(1, 1), 
                   pie="two")
dat <- rbind(dat1,dat2)
ggplot(dat, aes(x = pie, y = value, fill = year)) +
  geom_bar(stat = "identity", width = 1) +
  facet_grid(~pie,scales="free")+
  scale_fill_manual(values=c("#1CF900","#FFA600", "#FF4000"))

Created on 2024-12-19 with reprex v2.1.1

@clauswilke
Copy link
Member

I didn't use color codes as data values

You did. They're in your data frame. And then you chose to display them, via scale_fill_identity().

As I wrote in my first response, scale_fill_identity() is almost always the wrong choice, and you shouldn't use it unless you're an expert and know exactly what you're doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants