Skip to content

Parametrize r_rescale #6284

Closed
Closed
@KryeKuzhinieri

Description

@KryeKuzhinieri

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!

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