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

Parametrize r_rescale #6284

Open
KryeKuzhinieri opened this issue Jan 17, 2025 · 1 comment
Open

Parametrize r_rescale #6284

KryeKuzhinieri opened this issue Jan 17, 2025 · 1 comment

Comments

@KryeKuzhinieri
Copy link

We are facing issues to fill the blank space in pie charts using ggplot2_3.5.1 (issue in other versions too).

The following code:

library(ggplot2)

# Create Data
data <- data.frame(
  group=LETTERS[1:5],
  value=c(13,7,9,21,2)
)

# Basic piechart
ggplot(data, aes(x="", y=value, fill=group)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) +
  theme_gray() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.text.x = element_blank(),
  )

Gives the following graph:

Image

The issue seems to arise from the r_rescale function which has the donut = c(0, 0.4) hardcoded. This can be fixed by overriding the function as shown below:

library(scales)
library(ggplot2)

# Create Data
data <- data.frame(
  group=LETTERS[1:5],
  value=c(13,7,9,21,2)
)

custom_r_rescale <- function(x, range, donut = c(0, 0.49)) {
  x <- squish_infinite(x, range)
  rescale(x, donut, range)
}
assignInNamespace("r_rescale", custom_r_rescale, ns="ggplot2", pos="package:ggplot2")


ggplot(data, aes(x="", y=value, fill=group)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) +
  theme_gray() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.text.x = element_blank(),
  )

Image

However, this is a hacky way to solve this issue. It seems that the same issue was reported in a stackoverflow issue as well.

The question I have is if it would be possible to parametrize this parameter or this function?

Thanks a lot!

@teunbrand
Copy link
Collaborator

I don't think this will be implemented for coord_polar() as it has been superseded by coord_radial().
The reason there is extra space around the edge of the circular area is because it is a place reserved for axis labels.
For that reason, I wouldn't like (the option) to get rid of this space.

If you insist on ignoring the axis label space, you can override the relevant parameter in the coord like so:

# Add this to plot with `+`
  ggproto(
    NULL, coord_radial("y", start=0, expand = FALSE),
    inner_radius = c(0, 0.5)
  )

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

2 participants