R的良好轮廓/密度图

时间:2018-04-06 16:14:50

标签: r ggplot2 density-plot

我想为这个数据做一个轮廓图

        model1        model2
1  0.006889929 0.005679936
2  0.007700212 0.005249357
3  0.008565160 0.004499936
4  0.006881213 0.005151137
5  0.008636678 0.006632931
6  0.007141556 0.005622523
7  0.008368942 0.005443024
8  0.006818685 0.005273998
9  0.006696313 0.006221722
10 0.006268623 0.004654712
11 0.007999088 0.005780017
12 0.007532648 0.005547954
13 0.006554138 0.006242407
14 0.008131300 0.004914134
15 0.006231241 0.004716008
16 0.006426665 0.005245560
17 0.008686260 0.007035450
18 0.007636747 0.006508285
19 0.005485265 0.004489133
20 0.005694367 0.005601413
21 0.008493238 0.005385272
22 0.007729237 0.006110326
23 0.010172940 0.005355400
24 0.007146254 0.007183295
25 0.006360539 0.006388278
26 0.008034973 0.005276761
27 0.008438192 0.004589171
28 0.007425142 0.007708877
29 0.006258370 0.004478218
30 0.009314826 0.007461234
31 0.006842500 0.006158923
32 0.009193691 0.005661072
33 0.008708791 0.006167508
34 0.007306337 0.005066528
35 0.007275601 0.004493423
36 0.006115221 0.005373104
37 0.006952139 0.004571620
38 0.007906732 0.006197955
39 0.007330278 0.005857626
40 0.008920438 0.007038646
41 0.008047256 0.005619965
42 0.006813636 0.005175007
43 0.007514715 0.008053758
44 0.007525681 0.005289664
45 0.007197230 0.005243756
46 0.011364365 0.006323551
47 0.007843592 0.005399640
48 0.006785430 0.004375154
49 0.008328563 0.005376976
50 0.007069436 0.005173282

我使用代码

set.seed (123)
xvar <- sqrt(VarianceLOm1[9,])
yvar <-   sqrt(VarianceLOm2[9,])
myd <- data.frame (xvar, yvar)

p1 <- ggplot(myd,aes(x=xvar,y=yvar))+
  stat_density2d(aes(fill=..level..), geom="polygon") +
  coord_cartesian(c(0.003, 0.012), c(0.003, 0.012)) +
  theme(legend.position = "none")

gt <- ggplot_gtable(ggplot_build(p1))

grid.newpage()
grid.draw(gt)

产生密度图

enter image description here

但说实话,这看起来并不好看,因为它看起来很不完整。我想知道是否有更好的情节可供使用。它理想地需要2D,我已经看过3D密度图并且它们不是我需要的。

我查看了这个帖子以供参考,理想情况下我希望我的情节就像这个帖子中的情节但我似乎无法让它发挥作用。 (two-way density plot combined with one way density plot with selected regions in r

完整代码

p1 <- ggplot(myd,aes(x=xvar,y=yvar))+
  stat_density2d(aes(fill=..level..), geom="polygon") +
  coord_cartesian(c(0.003, 0.012), c(0.003, 0.012)) +
  theme(legend.position = "none")

p2 <- ggplot(myd, aes(x = xvar)) + stat_density() +
  coord_cartesian(c(0.003, 0.012))
p3 <- ggplot(myd, aes(x = yvar)) + stat_density() + 
  coord_flip(c(0.03, 0.012))

gt <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))

gt1 <- gtable_add_cols(gt, unit(0.3, "null"), pos = -1)
gt1 <- gtable_add_rows(gt1, unit(0.3, "null"), pos = 0)

gt1 <- gtable_add_grob(gt1, gt2$grobs[[which(gt2$layout$name == "panel")]],
                                 1, 4, 1, 4)
gt1 <- gtable_add_grob(gt1, gt2$grobs[[which(gt2$layout$name == "axis-l")]],
                                 1, 3, 1, 3, clip = "off")

gt1 <- gtable_add_grob(gt1, gt3$grobs[[which(gt3$layout$name == "panel")]],
                                 4, 6, 4, 6)
gt1 <- gtable_add_grob(gt1, gt3$grobs[[which(gt3$layout$name == "axis-b")]],
                                 5, 6, 5, 6, clip = "off")
grid.newpage()
grid.draw(gt1)

它看起来像这个

enter image description here

看到第二张图无处可寻,轴有点混乱

0 个答案:

没有答案