2014-01-10 1 views
6

I가이 코드R에 ggplot2로 만든 플롯의 y 축 스케일로 숫자에 대한 SI 접두어를 정확하게 표시하는 방법?

plt <- ggplot(d2, aes_string(x=names(same_df)[1],y= "value")) + 
    geom_point(aes(color = variable), size = 1)+ theme_bw()+ 
    theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+ 
    theme(axis.text=element_text(size=20)) + 
    theme(axis.title=element_text(size=20,face="bold")) + scale_color_discrete(name = "title", labels = c("1", "2", "3", "4","5","6","7","8","9")) + labs(x = "x", y = "y")+ guides(colour = guide_legend(override.aes = list(size=4),ncol=2,title.hjust=0.5))+theme(plot.margin=unit(c(0,0,0,0),"mm")) 
내가 다음 단계를했다는 것을 얻기 위해, y 축에서 숫자 SI 접두어 표기법을 필요로하지만

enter image description here

을 사용하여 생성 된 다음 플롯,

library("sos") 
는 sitools 패키지의 findFn을 사용하려면

findFn("{SI prefix}") 
,451,515,

은 그 때 나는

enter image description here

정확하게 f2si 동안, SI 접두어

plt2 <- plt + scale_y_continuous(labels=f2si) 

결과 줄거리는 다음과 같습니다과 번호로 부동 소수점 숫자 번호를 변환 할 라벨의 f2si를 사용 -1e^-0.8에 대한 y 축을 -10 n으로 변경하면 0과 1e^-0.8의 값이 각각 0과 10n으로 정확하게 표시되지 않습니다. 누군가가 여기에 수정해야 할 것을 제안하여 번호가 전체적으로 표시되도록 표시 할 수 있습니까?

감사합니다.

+0

참조 [여기] (http://stackoverflow.com/q/8281897/707145). – MYaseen208

답변

3

나는 당신의 행동을 재현 할 수 없었다. 이 참조 :

df <- data.frame(x=runif(100), y=(runif(100)-1/2)/1e8) 
p <- ggplot(df, aes(x, y)) + geom_point() 
p + scale_y_continuous(labels=f2si) 

enter image description here

당신이 "0 N"레이블이 마음에 들지 않는 경우 나, 또한 found 다른 유사한 기능을 가지고 :

format_si <- function(...) { 
    # Based on code by Ben Tupper 
    # https://stat.ethz.ch/pipermail/r-help/2012-January/299804.html 

    function(x) { 
    limits <- c(1e-24, 1e-21, 1e-18, 1e-15, 1e-12, 
       1e-9, 1e-6, 1e-3, 1e0, 1e3, 
       1e6, 1e9, 1e12, 1e15, 1e18, 
       1e21, 1e24) 
    prefix <- c("y", "z", "a", "f", "p", 
       "n", "µ", "m", " ", "k", 
       "M", "G", "T", "P", "E", 
       "Z", "Y") 

    # Vector with array indices according to position in intervals 
    i <- findInterval(abs(x), limits) 

    # Set prefix to " " for very small values < 1e-24 
    i <- ifelse(i==0, which(limits == 1e0), i) 

    paste(format(round(x/limits[i], 1), 
       trim=TRUE, scientific=FALSE, ...), 
      prefix[i]) 
    } 
} 

p + scale_y_continuous(labels=format_si()) 

enter image description here

+1

당신의 제안에 감사드립니다, 그것은 지금 작동합니다. 나는 0에 가까운 n을 제거하는 코드의 마지막 줄에서'p + scale_y_continuous (labels = format_si())'를 의미하는 것 같아요. – Amm

+0

예, 감사합니다. 편집 됨. – tonytonov

+0

필자가 작성한'format_si()'함수는 커스터마이징하기 쉽기 때문에, "G"대신에 "B"를 사용하면 수십억을 표현할 수있다. – coip

관련 문제