2016-10-21 3 views
0

Y 축에 백분율을 표시하고 싶습니다. 이 작업을 수행하는 가장 일반적인 방법은 ggplot2에서 비늘을 사용하는 것입니다 : scale_y_continuous(labels=percent).ggplot2의 축 비율 표시 : 'scale_y_continuous (labels = percent)'

나는 labels=every_nth, 사용자 정의 기능을 구현하고 있기 때문에 Y 축

library(ggplot2) 
library(scales) 

ggplot(all_mydata, aes(x=dose,y=probability, group=model))+ 
geom_point(aes(col=model, shape=model))+ 


#X-Axis transformation: 
    annotation_logticks(scaled = TRUE,sides="b") + 
    scale_x_log10(breaks = 10^(-1:10), 
       labels = trans_format("log10", math_format(10^.x)))+ 



    #Y axis minor ticks using "every_nth", for minor ticks. 
    scale_y_continuous(breaks=seq(0,1,0.05), 
        labels=every_nth(seq(0,1,0.05),2,inverse=TRUE)) 

# scale_y_continuous(labels=percent) #conflicts with previous scale_y_continuous call 

enter image description here

데이터에 작은 눈금을 표시하려면이 방법을 사용할 수 없습니다 입니다 -이 매우 관련이없는 case

all_mydata<-structure(list(dose = c(3, 3, 25, 25, 25, 50, 50, 50), total = c(25L, 
25L, 25L, 25L, 25L, 25L, 25L, 25L), affected = c(1L, 3L, 22L, 
14L, 22L, 23L, 16L, 21L), probability = c(0.04, 0.12, 0.88, 0.56, 
0.88, 0.92, 0.64, 0.84), model = c("mod1", "mod1", "mod1", "mod1", 
"mod1", "mod1", "mod1", "mod1")), .Names = c("dose", "total", 
"affected", "probability", "model"), row.names = c(1L, 2L, 51L, 
52L, 53L, 73L, 74L, 75L), class = "data.frame") 

스택 e에서 발견 된 사용자 정의 함수

every_nth <- function(x, nth, empty = TRUE, inverse = FALSE) 
{ 
    if (!inverse) { 
    if(empty) { 
     x[1:nth == 1] <- "" 
     x 
    } else { 
     x[1:nth != 1] 
    } 
    } else { 
    if(empty) { 
     x[1:nth != 1] <- "" 
     x 
    } else { 
     x[1:nth == 1] 
    } 
    } 
} 

답변

1

예를 들어, 당신은 그런 일에 every_nth 기능을 변경할 수 있습니다 Y 축에 작은 눈금을 그립니다 체인지는 :

every_nth <- function(x, nth, empty = TRUE, inverse = FALSE) 
{ 
    if (!inverse) { 
     if(empty) { 
      x[1:nth == 1] <- "" 
      out <- x 
     } else { 
      out <- x[1:nth == 1] 
     } 
    } else { 
     if(empty) { 
      x[1:nth != 1] <- "" 
      out <- x 

     } else { 
      out <- x[1:nth == 1] 
     } 
    } 
    out2 <- paste0(100*as.numeric(out), "%") 
    out2[out2 == "NA%"] <- "" 
    out2 
} 
+0

나는 그런 기능을 수정할 생각하지 않았다! 완벽하게 작동합니다, 감사합니다! – Arch