简体   繁体   中英

axis labels with comma but no decimals ggplot

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

带逗号和小数的图表

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

They are both from the scales package

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM