0

After running the following code, I receive the error:

Error in comma(count) : could not find function "comma"

ggplot(mydata, aes(xvar, fill=yvar)) + 
  geom_bar(alpha = 0.7) +
  scale_y_continuous(breaks = seq(0, 14000, by=1000), label = comma) +
  labs(title="title", x="xlab", y="ylab") +
  scale_fill_manual(values=c("gold2", "forestgreen", "dimgrey")) +
  theme(panel.background = element_rect(fill = 'gray95'),
        title = element_text(face = "bold.italic", color = "black"),
        plot.title = element_text(hjust = 0.5))  +
  geom_text(stat = "count", 
            aes(label = comma(..count..)), 
            size = 4,
            position = position_stack(vjust = 0.50))

Once I remove the comma() function from within geom_text(), the code runs fine and produces the graph I want, except the data labels are not in comma format. I have the 'scales' package loaded and even use label = comma in the scale_y_continuous() line. What am I doing wrong here?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Jblum
  • 1
  • 2
  • 1
    What version of `ggplot2` are you using? You can check with `sessionInfo()`. Also when asking for help, it's very helpful to provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Oct 04 '18 at 18:46
  • 2
    @BenBolker ; i deleted comments as the question notes that scales is loaded. @Chief ; just to confirm you have used `library(scales)` – user20650 Oct 04 '18 at 18:47
  • is https://stackoverflow.com/questions/7027288/error-could-not-find-function-in-r helpful/a duplicate? – Ben Bolker Oct 04 '18 at 18:48
  • 1
    @BenBolker That could be a duplicate, but there was an issue with ggplot and using the `..` variables in that it executed them in an empty environment so you couldn't use functions in connections with them. I thought that was only in an older ggplot versions but maybe it persists. So it may be a unique situation given that `comma` works with the `scale_y_continuous` in the same plot. – MrFlick Oct 04 '18 at 18:51
  • 1
    can't reproduce; if I run `mydata <- data.frame(xvar=1:3,yvar=factor(1:3)); library(ggplot2); library(scales)` and then your code, I get a (trivial) plot (ggplot2 version 3.0.0) – Ben Bolker Oct 04 '18 at 18:51
  • This is where I had faced a similar problem in the past: https://stackoverflow.com/questions/24538031/how-can-i-make-a-frequency-distribution-bar-plot-in-ggplot2/24541345#24541345 (but that was ggplot2_1.0.0) – MrFlick Oct 04 '18 at 19:01
  • @MrFlick I'm using ggplot2_2.2.1, is that too old? How do I update a package? I'd love to create a reproducible example, but I'm still new to R and unfamiliar with the commands for generating random samples / creating data frames. I'm just importing datasets I've already cleaned in SAS. – Jblum Oct 04 '18 at 19:10
  • The original comment I left had a link with ways to create a reproducible example. Using something like `dput()` is usually most effective. You can usually just update a package by installing it again. `install.packages("ggplot2")` – MrFlick Oct 04 '18 at 19:12
  • @user20650 I have already ran library(scales). – Jblum Oct 04 '18 at 19:12
  • 1
    @MrFlick It seems the fix was as simple as updating to the newest version of R and ggplot2 (hindsight 20/20). Thanks for the help, I'll be sure to create a reproducible example next time. – Jblum Oct 04 '18 at 19:31

0 Answers0