2016-11-01 63 views
0

我創建了以下情節x軸:畫出GGPLOT2平滑線在y軸,而不是

數據:

lat<-c(37.30,37.30,37.30,37.30,69.25,69.25,37.30,0.00,0.00,37.30,37.30,37.30,37.30,-75.00,-75.00,70.00,25.30,25.30,37.30,45.00,46.75,-49.00,-49.00,-49.00,58.50,-37.00,37.30,37.30,37.30,37.30,69.25,69.25,37.30,0.00,0.00,37.30,37.30,37.30,37.30,-75.00,-75.00,70.00,25.30,25.30,37.30,-49.00,-49.00,-49.00,16.10,-9.12,50.00,30.00) 
    prop<-c(64, 62, 38, 37, 50, 30, 27, 10, 25, 39, 25, 6, 5, 25, 47, 24, 20, 2, 62, 40, 48, 60, 20, 40, 66, 57, 14, 25, 11, 0, 3, 5, 0, 0, 0, 14, 10, 11, 9, 1, 1, 34, 20, 90, 0, 0, 0, 0, 4, 5, 85, 6) 
    group<-c("gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr") 
    mydata<-data.frame(lat,prop,group) 

圖表:

library(ggplot2) 
    library(scales) 

    ggplot(data=mydata,aes(x=lat,y=prop,colour=group))+ 
    geom_point(size=2.3) + 
    stat_smooth(span=0.75,n=26,method="loess",aes(fill=group)) + 
    theme_bw()+ 
    theme(
     panel.grid.minor=element_blank(), 
     panel.grid.major=element_blank(), 
     legend.position=c(0.15,0.85), 
     legend.title=element_blank(), 
     axis.text=element_text(size=17), 
     axis.title=element_text(size=19), 
     legend.text=element_text(size=17) 
    )+ 
    scale_y_continuous(limit=c(0,NA),oob=squish)+ 
    scale_x_continuous(limit=c(-70,70),oob=squish,breaks=c(-60,-30,0,30,60)) 

然而,這不是我想要的最終情節。我想保留黃土迴歸的輸出,如上圖所示,但在x軸和y軸顯示prop。我知道對於統計來說,正確的做法是在y軸上保留因變量(即prop),但是我的自變量(即lat)表示緯度,並且在垂直位置顯示緯度會很有趣(y -軸)。如果我簡單地更改以下代碼行,說明x = prop和y = lat,我的黃土退化將尊重這個聲明,並且不會產生所需的擬合(即prop作爲lat的函數)。

ggplot(data=mydata,aes(x=prop,y=lat,colour=group))+ 

如何重現以上(即,維持黃土迴歸因爲它們),但在X軸和支柱LAT在y軸完全情節任何想法?

+3

嘗試加入'coord_flip()'你'ggplot結束()'調用 – mtoto

+0

可能的複製[ ggplot2:geom \ _smooth select觀察連接(等同於geom \ _path())](http://stackoverflow.com/questions/39476914/ggplot2-geom-smooth-select-observations-connections-equivalence-to-geom-path ) – Axeman

回答

0

雖然stat_smooth無法直接執行此操作,但您可以簡單地使用coord_flip來獲取所需內容。你基本上構建了你想要的圖,然後翻轉x軸和y軸。在下面的例子gg是代表您所提供的ggplot代碼ggplot2對象:

gg + coord_flip() 

enter image description here

+0

謝謝你,完美的作品! – Suzana

+0

如果這回答您的問題,您可以通過點擊我答案旁邊的up/downvote計數器旁邊的綠色勾號來接受它。 –

+0

我做到了,它說我的投票是記錄的,但沒有出現在這裏,因爲我沒有足夠的聲譽,對不起。 – Suzana