Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making pie charts with no axis numbering/ticks in ggplot2

Tags:

r

ggplot2

I've been learning my way through ggplot2, and I've made it to using polar coordinates and making pie charts and I've run into a bit of trouble.

I want to make a pie chart that has no axis numbering or ticks. A sample of code I have that I thought should work is:

data = data.frame(Category = c("A", "B", "C", "D"), Value = runif(4))
ggplot(data, aes(0, weight = Value, fill = Category)) +
  scale_x_continuous(breaks = NA) +
  scale_y_continuous(breaks = NA) +
  geom_bar(binwidth = 1) +
  coord_polar(theta = "y") +
  scale_fill_brewer(pal = "Set1")

This code gives me the error:

Error in if (ends_apart < 0.05) { : argument is of length zero

Omitting the breaks argument in the scale_y_continuous function results in a successful plot, except with numbering and tick marks on the radius of the pie chart. Omitting the coord_polar (and leaving the breaks argument in scale_y_continuous) function results in a stacked bar chart without x or y numbering or tick marks.

I've found some solutions that involve changing tick mark options, and those should be a fine workaround, but I was curious as to why I'm getting this error.

As a side note: I've uninstalled and reinstalled ggplot2 to ensure that I have the latest version and that the checksums all match up.

Edit: To clarify, what I'm after is something like:

plot

except without the numbering on the pie chart.

like image 487
tkmckenzie Avatar asked Feb 20 '23 03:02

tkmckenzie


1 Answers

I think this is what you're after:

ggplot(data,aes(x = factor(0),y = Value,fill = Category)) + 
    geom_bar(stat = "identity",position = "fill") +
    scale_fill_brewer(palette = 'Set1') + 
    coord_polar(theta = "y") + 
    opts(axis.ticks = theme_blank(),
             axis.text.y = theme_blank(),
             axis.text.x = theme_blank())

Note: Since version 0.9.2 opts has been replaced by theme:

+ theme(axis.ticks = element_blank(), 
        axis.text.y = element_blank(),
        axis.text.x = element_blank()) 
like image 54
joran Avatar answered Feb 23 '23 15:02

joran