56

I'm plotting a fairly simple chart using ggplot2 0.9.1.

x <- rnorm(100, mean=100, sd = 1) * 1000000
y <- rnorm(100, mean=100, sd = 1) * 1000000
df <- data.frame(x,y)

p.new <- ggplot(df,aes(x,y)) +
  geom_point()
print(p.new)

Which works, but ggplot2 defaults to scientific notation that is inappropriate for my audience. If I want to change the x-axis label format by entering:

p.new + scale_x_continuous(labels = comma)

I get:

Error in structure(list(call = match.call(), aesthetics = aesthetics, : object 'comma' not found

What am I doing wrong? I note that the language changed recently from "formatter" to "labels". Perhaps I'm misreading the man page?

Edit: I was indeed misreading the man page

Need to load library(scales) before attempting this.

UseR10085
  • 7,120
  • 3
  • 24
  • 54
mediaczar
  • 1,960
  • 3
  • 18
  • 23
  • Good thought, but that seems to create a 1 item list that creates problems of its own:`Error in scale_labels.continuous(scale, major) : Breaks and labels are different lengths` – mediaczar Aug 16 '12 at 12:54
  • Can you make your example reproducible? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Aug 16 '12 at 13:03
  • @RomanLuštrik - just thought of that (also hoped that I might fix the problem simply by approaching from another angle... no such luck.) – mediaczar Aug 16 '12 at 13:09
  • 6
    Solved: need to load `library(scales)` before trying this. Slapping head. – mediaczar Aug 16 '12 at 13:12
  • 2
    You can post this as an answer and check it as the correct answer. You may need to wait some time before you can answer your own question. – Roman Luštrik Aug 16 '12 at 13:23

3 Answers3

69

One needs to load library(scales) before attempting this.

mediaczar
  • 1,960
  • 3
  • 18
  • 23
6

More generally, you can control some nice parameters using the "scales" package. One of its functions is number_format().

library(ggplot2)
library(scales)
p <- ggplot(mpg, aes(displ, cty)) + geom_point()

For formatting your numbers, you can use the function number_format(). It offers some nice possibilities, such as controlling the number of decimals (here 2 decimals) and your decimal mark (here ',' instead of '.')

p + scale_y_continuous(
  labels = scales::number_format(accuracy = 0.01,
                                 decimal.mark = ','))
Rtist
  • 3,825
  • 2
  • 31
  • 40
2

Here is an example of how to add commas and decimals to ggplot using scales::comma_format().

Essentially allowing for a prettyNum() style of formatting.

Seatbelts_df <- as.data.frame(Seatbelts)

ggplot(data=Seatbelts_df, aes(x=Seatbelts_df$drivers, y=Seatbelts_df$DriversKilled, color=factor(Seatbelts_df$law))) +
  geom_jitter(alpha=0.5) +
  theme(plot.title=element_text(face="bold")) +
  labs(title="Amount of Drivers on Road vs Amount of deaths", subtitle = "Dataset from package datasets::Seatbelts", x ="Drivers on Road", y="Amount of Deaths", color="Seatbelt Law?") +
  scale_color_manual(labels = c("Yes", "No"), values = c("blue", "red")) +
  geom_vline(aes(xintercept=mean(Seatbelts_df$drivers)), color="black", linetype="dashed", size=1) +
  scale_x_continuous(
  labels = scales::comma_format(big.mark = ',',
                                 decimal.mark = '.'))

example of output with commas

Gabriel Fair
  • 4,081
  • 5
  • 33
  • 54