2017-12-26 44 views
1

5 자리 이상의 숫자에 쉼표가있는 발행 스타일 가이드를 따르려고합니다. 이것을 찾았지만 'labels = comma'를 사용할 때 기본값을 덮어 쓰는 방법을 찾지 못했습니다. 아래의 예는 다음과 같습니다ggplot2의 축 레이블에 9999보다 큰 숫자 만 쉼표가 표시되도록 축 레이블

require(dplyr) 
require(ggplot2) 
require(scales) 

# create mock dataframe 
temp <- mpg %>% mutate(newvar=(hwy*300)) 

ggplot(temp, aes(x=cyl, y=newvar)) + geom_point() + 
scale_y_continuous(labels=comma) + 
labs(title="When using 'labels=comma'...", 
     subtitle="How format axis labels such that commas only appear for numbers > 9999?") 

이 예제를 사용하여, "6000"등을 수동으로이를 달성 할 수 "4000"를 읽을 수있는 최저 y 축 레이블을 싶지만 많은 그래프를 가지고 즉, 귀찮게 가치가 없어 이 범위를 포괄하는 비늘로 어떤 제안?

답변

6

우리는 scale_x_continuous 내에서 익명 함수를 사용할 수 있습니다

library(scales) 
library(ggplot2) 

# generate dummy data 
x <- 9998:10004 
df <- data.frame(x, y = seq_along(x)) 

ggplot(df, aes(x = x, y = y))+ 
    geom_point()+ 
    scale_x_continuous(labels = function(l) ifelse(l <= 9999, l, comma(l))) 

enter image description here