zach
zach

Reputation: 31073

Change the axes on a radial barchart

I want to make a radial stacked barchart. I have something like this:

ggplot(diamonds, aes(clarity, fill= cut)) +  geom_bar()  + coord_polar()

which yields a plot like this: radial barchart

However this is very crowded. Is there a way to change the axes so that this barchart is hollow? I would want zero to start not at the center of the circle but, say, 1/3 or 1/2 of the radius from the center. Any ideas about that?

Upvotes: 2

Views: 2127

Answers (2)

snaut
snaut

Reputation: 2535

I solved a similar problem with an axis transformation. What this transformation does is:

  • offset the y-axis 0-point by offset
  • transform the radius with sqrt so that the areas of the bars correspond to the values.

I used this to plot histograms of times of the day, the offset makes small values way better visible. Of course circular plots only really make sense for seasonal data, direction data, ...

require(ggplot2)
require(scales)

offset=2000
my_trafo_trans <- function() trans_new(
  "my_trafo",
  function(y){
    sign(y)*sqrt(abs(y))+offset^2
  }, 
  function(r){
    r^2+offset^2
  }
  )

ggplot(diamonds, aes(clarity, fill= cut)) +  
  geom_bar()  + 
  scale_y_continuous(limits=c(-1*offset, 15000)) + 
  coord_trans(y = "my_trafo") +
  coord_polar()

result of plot command

Upvotes: 0

Andrie
Andrie

Reputation: 179558

You can tell coord_plot to expand slightly - this puts a small hole in the middle:

ggplot(diamonds, aes(clarity, fill= cut)) +  
    geom_bar()  + 
    coord_polar(expand=TRUE)

enter image description here

Then you can control the y scale expansion (with the argument expand=... to scale_y_continuous(...). Unfortunately I think the expansion is symmetrical, i.e. if you add space at the bottom (i.e. in the middle, you also add it at the top (i.e. the outside):

ggplot(diamonds, aes(clarity, fill= cut)) +  
    geom_bar()  + 
    coord_polar(expand=TRUE) + 
    scale_y_continuous(expand=c(0.5, 0))

enter image description here

Upvotes: 1

Related Questions