1
votes

I created a piechart using ggplot2 library as follows:

ggplot(dat,
       aes(x = factor(""), fill = workers) ) +
       geom_bar() +
       coord_polar(theta = "y") +
       scale_x_discrete("")

The problem is that the numbers (count) are shown as 2e+05. How can I show them as 20000?

Update:

dat <- data.frame(workers=c("Q8","A2","S1","A2","A2","S1"))
1
You should post a reproducible example. The answer is probably along the lines of layering scale_y_continuous with some label argument while specifying what formatting the variable count should take, possibly by a call to format.shayaa
@shayaa: I added the reproducible example, but this example does not obviously show the issue with e+05, because I cannot post so many rows.FiofanS
cant reproduce your formatting, even when replicating your data a million times to make the counts in the 1e6 range. Your problem might be scipenin options.shayaa
Are you using the library(scales) ?Enrique
@Enrique: yes, I tried to use it. But not sure if it's the best option to achieve the required result.FiofanS

1 Answers

5
votes

You can use scales::comma to handle this (and also easily make a reproducible example, even with a large amount of data):

library(ggplot2)

dat <- data.frame(
  workers = c(
    rep("Q8", 100000),
    rep("A2", 200000),
    rep("S1", 200000)
  )
)

ggplot(dat, aes(x = factor(""), fill = workers) ) +
  geom_bar() +
  coord_polar(theta = "y") +
  scale_x_discrete(name="") +
  scale_y_continuous(label=scales::comma) +
  theme_bw() +
  theme(panel.grid=element_blank()) +
  theme(panel.border=element_blank()) +
  theme(axis.ticks=element_blank())

which produces this monstrosity:

Note that you really should not display the "y" axis numbers since they make no sense in this context and should either direct label the pie slices or add the value #'s to the actual legend labels.

But, might I suggest turning to a different vis:

library(waffle)
library(dplyr)

count(dat, workers) %>% 
  mutate(trim=n/10000) -> df2

parts <- setNames(unlist(df2$trim), df2$workers)  

waffle(parts, rows=4, title="Count", xlab="Each square represents 10,000 workers")

enter image description here

If you're using the github version of ggplot2 you'll need to do:

waffle(parts, rows=4, title="Count", xlab="Each square represents 10,000 workers") +
  scale_fill_manual(name="workers",
                    values=c(Q8="#e41a1c", A2="#377eb8", S1="#4daf4a"), 
                    na.translate=FALSE)

as it has some different behaviour than the CRAN version (the waffle pkg will be updated when the new ggplot2 is on CRAN).