2014-01-16 57 views
1

我有兩個功能y1y2強制ggplot顯示2曲線功能

如果我單獨繪製它們沒有問題(參見兩個第一圖)。

但是,如果我將它們合併,形狀看起來是線性的(參見圖3)。

我該如何解決這個問題?

x <- seq(0, 50, 1) ;x 
y1 <- exp(8.9191)+exp^(0.03307*x) 
y2 <- exp(9.9191)+exp^(0.06307*x) 
df <- data.frame(x,y1,y2) 

require(ggplot2) 

ggplot(df, aes(x)) +     
    geom_line(aes(y=y2), colour="red") #Looks nice (curvy) 

ggplot(df, aes(x)) +     
    geom_line(aes(y=y1), colour="blue") #Looks nice (curvy) 

ggplot(df, aes(x)) +      
    geom_line(aes(y=y1), colour="blue") + 
    geom_line(aes(y=y2), colour="red") #Looks not nice linear 

enter image description here

enter image description here

enter image description here

+3

...因爲他們在巨大的不同尺度?這幾乎是不可能調和的:因爲有很大的加性常數,所以即使增加一個對數垂直尺度也沒有多大幫助。 –

+0

你想比較什麼?也許有更好的方式來表示數據。 –

+0

紅色曲線代表女性和藍色男性。所以我試圖比較男人和女人的區別。在我的例子中x軸沒有任何表示。但是,您可以想象,x軸是工作經驗,y軸可以代表工資。 – S12000

回答

3

也許你可以使用facet_grid()和尺度的選擇。 您將有兩個方面,每個方面都有不同的y尺度。

require(reshape2) 
mdf <- melt(data.frame(y1, y2)) 
mdf$x <- x 

ggplot(mdf, aes(x = x)) + 
    geom_line(aes(y = value)) + 
    facet_grid(variable ~ ., scales = "free_y") 

enter image description here

+0

謝謝,我想這確實是最好的解決方案。 – S12000