大家好:我想使用R plotly在2D Density Contour Plot中绘制kernel density而不是histograms。有人可以帮帮我吗?以下是我的数据和代码。
set /?
答案 0 :(得分: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)