Home > Software design >  How to modify the limits of y axis of a stacked bar chart in polar coordinates using ggplot2?
How to modify the limits of y axis of a stacked bar chart in polar coordinates using ggplot2?

Time:09-22

I have ploted a stacked bar chart with "position = stack" in polar coordinates.

ggplot(d, aes(Component, Freq, fill = Subtype2))  
  geom_bar(position = 'stack', stat = 'identity')  
  scale_x_discrete(expand = c(0.03, 0.5))  
  scale_y_continuous('Distribution', expand = c(0, 0))  
  scale_fill_nejm()  
  guides(fill = guide_legend(title = 'IHC subtype'))  
  coord_polar(theta = 'y')  
  theme(panel.background = element_blank(), 
        plot.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.line = element_line(size = 0.25),
        axis.ticks = element_line(size = 0.25),
        axis.title = element_text(size = 12),
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(size = 10))

enter image description here

To add a space between the start and the end of y axis, I add limits = c(0, 2000) in scale_y_continuous.

ggplot(d, aes(Component, Freq, fill = Subtype2))  
  geom_bar(position = 'stack', stat = 'identity')  
  scale_x_discrete(expand = c(0.03, 0.5))  
  scale_y_continuous('Distribution', expand = c(0, 0), limits = c(0, 2000))  
  scale_fill_nejm()  
  guides(fill = guide_legend(title = 'IHC subtype'))  
  coord_polar(theta = 'y')  
  theme(panel.background = element_blank(), 
        plot.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.line = element_line(size = 0.25),
        axis.ticks = element_line(size = 0.25),
        axis.title = element_text(size = 12),
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(size = 10))

enter image description here

Next, I want to plot a proportional bar chart in polar coordinates and I tried the code below:

ggplot(d, aes(Component, Freq, fill = Subtype2))  
  geom_bar(position = 'fill', stat = 'identity')  
  scale_x_discrete(expand = c(0.03, 0.5))  
  scale_y_continuous('Distribution', expand = c(0, 0))  
  scale_fill_nejm()  
  guides(fill = guide_legend(title = 'IHC subtype'))  
  coord_polar(theta = 'y')  
  theme(panel.background = element_blank(), 
        plot.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.line = element_line(size = 0.25),
        axis.ticks = element_line(size = 0.25),
        axis.title = element_text(size = 12),
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(size = 10))

enter image description here

I want to add a space between the start and the end of y axis as well with the following code but the output was weird with warning message.

ggplot(d, aes(Component, Freq, fill = Subtype2))  
  geom_bar(position = 'fill', stat = 'identity')  
  scale_x_discrete(expand = c(0.03, 0.5))  
  scale_y_continuous('Distribution', expand = c(0, 0), limits = c(0, 100))  
  scale_fill_nejm()  
  guides(fill = guide_legend(title = 'IHC subtype'))  
  coord_polar(theta = 'y')  
  theme(panel.background = element_blank(), 
        plot.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.line = element_line(size = 0.25),
        axis.ticks = element_line(size = 0.25),
        axis.title = element_text(size = 12),
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(size = 10))
Warning message:
Removed 6 rows containing missing values (position_stack). 

enter image description here

data

structure(list(Component = structure(c(11L, 10L, 9L, 8L, 11L, 
10L, 9L, 8L, 11L, 10L, 9L, 8L, 11L, 10L, 9L), .Label = c("10", 
"9", "8", "7", "6", "5", "4", "3", "2", "1", "0"), class = "factor"), 
    Subtype2 = structure(c(3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 4L, 4L, 4L), .Label = c("HR /HER2-", "HR /HER2 ", 
    "HR-/HER2 ", "TNBC"), class = "factor"), Freq = c(12L, 433L, 
    67L, 66L, 87L, 461L, 287L, 84L, 17L, 332L, 60L, 32L, 44L, 
    278L, 159L)), row.names = c(1L, 2L, 3L, 4L, 12L, 13L, 14L, 
15L, 23L, 24L, 25L, 26L, 34L, 35L, 36L), class = "data.frame")

CodePudding user response:

This was a bit trickier than I imagined. The y-scale seems to want to throw away values that exceed the limits before the position is calculated. To prevent this, you can use scales::oob_keep() to retain these values. Simplified example below.

library(ggplot2)

# d <- structure(...) # taken from question, omitted for brevity

ggplot(d, aes(Component, Freq, fill = Subtype2))  
  geom_col(position = "fill")  
  scale_y_continuous(limits = c(0, 1.1), oob = scales::oob_keep)  
  coord_polar(theta = "y")

  • Related