2017-11-21 1 views
0

안녕하세요 : R을 플롯으로 사용하여 2D 밀도 윤곽 플롯에서 histograms 대신 kernel density을 그려야합니다. 아무도 도와 줄 수 없습니까? 아래는 나의 데이터와 코드입니다. 다음과 같이플롯으로 히스토그램 대신 KDE를 사용하는 2D 밀도 윤곽 플롯

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

당신은 커널 밀도 추정을 생성하는 density 기능을 사용하고 선 그래프로 이러한 플롯 할 수 있습니다 :

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