Home > Software design >  How can I create an "inner ring" for labels when using polar coordinates in ggplot a with
How can I create an "inner ring" for labels when using polar coordinates in ggplot a with

Time:09-22

I can use ggplot and coord_polar()and ylim(-2000, 500) to create a plot like this: polar plot with inner ring to show the "x-axis" group labels which has an empty inner ring which displays the group labels on the "former" x-axis.

But when I have a wider range of numbers and use scale_y_continuous(trans='log10'), I get this: polar plot with log scale... no inner ring in which all the bars start at the very center (origin) with now way to insert the group labels in a blank inner ring. I've tried numerous numerous things to get an empty inner ring (in which to display group labels), but it appears log scaled polar ggplots don't permit this, at least not trivially. Any way to accomplish this?

CodePudding user response:

The trick in this case is not to set the limits to a negative value, but raise a positive number to a negative power. You may recall that log(0) evaluates to -Inf and log(-1) evaluates to NaN. Example with a standard dataset below.

library(ggplot2)

ggplot(mpg, aes(interaction(year, cyl)))  
  geom_bar()  
  annotate(
    "segment",
    x = c(0.55, 2.55, 3.55, 5.55),
    xend = c(2.45, 3.45, 5.45, 7.45),
    y = 1e0, yend = 1e0, size = 2
  )  
  annotate(
    "text",
    x = c(1.5, 3, 4.5, 6.5),
    y = 5e-1, label = c("4", "5", "6", "8")
  )  
  scale_y_log10(limits = c(1e-1, NA))  
  coord_polar()

Created on 2021-09-16 by the reprex package (v2.0.1)

  • Related