2

I want y-axis labels that have a comma but no decimal points. When I use the 'labels=comma' option in ggplot I get the comma, it also adds in 2 decimals points. When I remove the option I get rid of the decimal points but also lose the comma.

ggplot(birth_decade_data, aes(x = age, y = disp_inc, group = birth_decade, col = birth_decade))+
  geom_point(size=2)+
  geom_line(size=1.05)+
  scale_y_continuous(expand=c(0,0), limits = c(0,80000), breaks=c(20000,40000,60000,80000), labels=comma)+
  scale_x_continuous(expand = c(0.01, 0.01), breaks = c(seq(15,65,5)), limits = c(24,65))

Thanks in advance for your help, I've spent ages searching for a solution to this.

Sorry, data looks like this:

> birth_decade_data

year birth_decade disp_inc   age
1989 1940s           35161 44.1 
1989 1940s           35161 44.1 
1989 1950s           31285 34.5 
1989 1960s           37403 25.0 
1989 1970s           33117 17.7 
1989 1980s           26700  7.00
1999 1940s           39448 54.2 
1999 1950s           38645 44.3 
1999 1960s           38617 34.6 
1999 1970s           41514 24.4 
1999 1980s           33972 16.7 

... more rows

chart with commas and decimals

neilfws
  • 32,751
  • 5
  • 50
  • 63
HenryW
  • 21
  • 1
  • 3
  • Please include at least a sample of `birth_decade_data`: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – neilfws Jun 05 '18 at 00:07
  • 2
    I'm unable to reproduce the issue using the example data - I don't get decimal places. For other users, note that `comma` is supplied by the `scales` package. – neilfws Jun 05 '18 at 00:52
  • 2
    Try replacing `comma` with `comma_format(digits = 0)`? Can't guess further without a reproducible example. – Z.Lin Jun 05 '18 at 01:44
  • I'm also unable to reproduce the issue. I tried with `disp_inc` as `numeric`, `double`, and `integer`, and no decimals as you've described. Can you confirm that, loading in the example data you've provided exactly as it is, you see the decimal behavior? (As opposed to working with a larger data frame that you've just excerpted here) – andrew_reece Jun 05 '18 at 02:02
  • @Z.Lin, thanks that fixes the problem. Thanks to others for your help and apologies the example wasn't reproducible. – HenryW Jun 05 '18 at 04:05

2 Answers2

4

If you use labels=comma_format() instead of label=comma that should work.

They are both from the scales package

Joe
  • 8,073
  • 1
  • 52
  • 58
2

Use the scales R package and add the following to your scale_y_continuous function: labels=comma_format(accuracy=1) instead of labels=comma or labels=comma_format(digits=0). I tried the digits=0 first but got a warning that it is now deprecated and to use accuracy instead. I was able to remove the decimal space with accuracy=1 in my own plot. The notes say that ggplot2 rounds to this number, so using accuracy=1 effectively rounds to integer values.

Tania
  • 136
  • 1
  • 4