2013-10-08 3 views
4

R (Ver 3.0.2)에서 ggplot2 (Ver 0.9.3.1)를 사용하여 로그 - 로그 플롯에 세 가지 함수를 플로트하고자합니다.ggplot의 선형 함수를 로그 - 로그 스케일로 플롯

y = x 
y = 0.5*x 
y = 1.5*x 

저는 많은 문제를 시도했지만 문제가 계속되었습니다. 여기에는 stackoverflow 질문 here에 대한 읽기가 포함됩니다.

내가 갖고 싶은 예제는 here입니다. Matlab에서 그 음모를 만들었습니다.

다음은 현재 작업하고있는 코드 샘플이지만 현재는 아무 것도 쓰지 않습니다. 궁극적으로, 다른 데이터 (구조를 보여주기 위해 로그 로그가 필요함) 위에 레이어가되고 싶습니다.

library(ggplot2) 

plot = ggplot() 
plot = plot + coord_cartesian(xlim = c(0.02, 300), ylim = c(0.035, 20)) 
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {x}, aes(x,y), geom = "line", color = "blue") 
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {0.5*x}, aes(x,y), geom = "line", color = "red") 
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {1.5*x}, aes(x,y), geom = "line", color = "red") 
plot = plot + scale_x_log10() + scale_y_log10() + annotation_logticks() 
plot 
+0

왜 aes (x, y) 대신에 aes (log (x, 10), log (y, 10))를 사용합니까? –

답변

8

당신은 coord_trans 대신 scale_.._log10

ggplot(data = data.frame(x=c(0.0001,1000), y=c(0.001,1)), aes(x=x,y=y)) + 
    stat_function(fun = function(x) x, geom='line',colour ='blue') + 
    stat_function(fun = function(x) 0.5*x, geom='line',colour = 'red') + 
    stat_function(fun = function(x) 1.5 * x , geom = 'line', colour = 'red') + 
    coord_trans(xtrans = 'log10',ytrans = 'log10', limx = c(0.02,300), limy =c(0.035,20)) + 
    annotation_logticks(scaled=FALSE) + 
    scale_x_continuous(breaks = trans_breaks("log10", function(x) 10^x), 
       labels = trans_format("log10", math_format(10^.x))) + 
    scale_y_continuous(breaks = trans_breaks("log10", function(x) 10^x), 
        labels = trans_format("log10", math_format(10^.x))) 

enter image description here

?annotation_logticks 제공 (적절하게 표시된 휴식을 얻을 수 annotation_logticks에서 예제를 사용하여) 같은 coord_cartesian

뭔가를 사용할 수 있습니다 이 issu에 접근의 숫자 e

+0

좋아요! 그 코드를 넣은 다음 다른 코드를 추가했는데 작동 중입니다 ... 의견을 보내 주셔서 감사합니다! –

관련 문제