2017-11-21 6 views
0

こんにちはすべて:私はプロットを使って2D濃度コンタープロットでhistogramsの代わりにkernel densityを描きたいと思います。誰も助けてくれませんか?以下は私のデータとコードです。2D濃度コンタープロットでヒストグラムの代わりにKDEをプロットする

あなたはカーネル密度推定値を生成する density機能を使用してから、次のように線グラフとしてこれらをプロットすることができ
g <-c("Pla", "Ond","Gra", "Dol","Tro", "Ond+Dex", "Pal","Ram", "Ond+Drop", "Ond+Met", "Gra+Dex", "Pal+Dex", "Dol+Dex", "Dol+Drop", "Gran+Drop") 
s1<-c(51.9, 64.9, 93.5, 27.7, 35.3, NA, NA, NA, NA, NA, NA, NA, 26.6, NA, NA) 
s2<-c(0.8, 25.4, 44.8, 13.3, 23.2, 71.9, 54.9, 51.3, 65.4, 52.8, 81.2, 43.7, 72.8, 76.8, 71.7) 
s3<-c(0.1, 20.1, 42.5, 37.7, 16.3, 63, 72.3, 34.9, 76.9, NA, 86.3, 67, NA, 71.9, 61.1) 
mydata<-data.frame(g, s1, s2, s3) 
rownames(mydata) <- mydata[,1] 
mydata <- mydata[,-1] 

s <- subplot(
    plot_ly(mydata, x = ~s1, type = "histogram"), 
    plotly_empty(mydata), 
    plot_ly(mydata, x = ~s1, y = ~s2, z = ~s3, type = "contour"), 
    plot_ly(mydata, y = ~s2, type = "histogram"), 
    nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2), margin = 0, 
    shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE 
) 
p <- layout(s, showlegend = FALSE) 
+0

たぶん、この記事がお手伝いしますhttps://stackoverflow.com/a/38769294/6256482 –

答えて

1

den_x <- density(mydata$s1, na.rm=TRUE) 
den_x <- data.frame(x=den_x$x, y=den_x$y) 

den_y <- density(mydata$s2, na.rm=TRUE) 
den_y <- data.frame(x=den_y$x, y=den_y$y) 

s <- subplot(
    plot_ly(den_x, x = ~x, y = ~y, type = "scatter", mode="lines"), 
    plotly_empty(mydata), 
    plot_ly(mydata, x = ~s1, y = ~s2, z = ~s3, type = "contour"), 
    plot_ly(den_y, x = ~y, y = ~x, type = "scatter", mode="lines"), 
    nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2), margin = 0, 
    shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE 
) 
p <- layout(s, showlegend = FALSE) 
+1

多くのありがとう、@aocall。それは動作します。 – Krantz

関連する問題