Reputation: 31073
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:
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
Reputation: 2535
I solved a similar problem with an axis transformation. What this transformation does is:
offset
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()
Upvotes: 0
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)
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))
Upvotes: 1