Skip to content

Parametrize r_rescale #6284

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

Closed
KryeKuzhinieri opened this issue Jan 17, 2025 · 4 comments
Closed

Parametrize r_rescale #6284

KryeKuzhinieri opened this issue Jan 17, 2025 · 4 comments

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)
  )

@KryeKuzhinieri
Copy link
Author

Thanks, your suggestion reaches the same result shown in my code. While it is again a hacky way to solve the problem, it does solve it.

Take care!

@r2evans
Copy link
Contributor

r2evans commented Feb 23, 2025

I just visited here jumping from https://stackoverflow.com/a/79461775/3358272 ...

@teunbrand,

The reason there is extra space around the edge of the circular area is because it is a place reserved for axis labels

We can control the presence of axis-labels for non-radial plots directly, such as with scale_x_continuous(name=NULL, breaks=NULL) (other ways, perhaps), is there a reason you're averse to doing this for radial as well? (other than adding coord_radial() with that scale_x can result in an error "EXPR must be a length 1 vector"`.

I apologize if I'm misunderstanding the ability of the ggproto(.) call above.

@teunbrand
Copy link
Collaborator

is there a reason you're averse to doing this for radial as well?

No the same breaks = NULL mechanism should work for polar/radial coords as well. If they don't, that is probably a (separate) bug. I can see this happening so I'll reopen #6258.

This particular issue is even when guides(theta = "none"), we need to reserve space for axis placement, because the data transformation cannot depend on the state of the guides. This isn't really a problem for cartesian systems as on the gtable level we adjust the cells, but that doesn't work for the circular 'panel' shapes.

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

3 participants