缩放stat_density级别

问题描述 投票:0回答:1

我有以下数据,称为grid

    conc    viscos     temp process filtration    psize speed
1     23 148.73609 23.78377       3          1 55.64441     2
1.1   23 128.28465 23.19413       3          1 58.68403     2
1.2   23 154.57065 27.37000       3          1 54.77725     2
1.3   23 154.68350 25.75547       3          1 54.76047     2
1.4   23 123.89097 27.00756       3          1 59.33705     2
1.5   23 174.87409 29.25369       3          1 51.75963     2
1.6   23 113.20778 23.67193       3          1 60.92485     2
1.7   23  99.76474 27.88921       3          1 62.92283     2
1.8   23 139.65546 28.21860       3          1 56.99403     2
1.9   23 108.02552 23.45404       3          1 61.69506     2

structure(list(conc = c(23, 23, 23, 23, 23, 23, 23, 23, 23, 23), 
viscos = c(148.736087893612, 128.284646104976, 154.570647098922, 54.68349762674, 123.890966907047, 174.874091619764, 113.207780061696, 99.7647376022436, 139.655456470316, 108.025515976116), 
temp = c(23.7837716944317, 23.1941282546014, 27.3700015971809, 25.7554684507881, 27.0075589349605, 29.2536879749291, 23.6719254390264, 27.8892069496691, 28.21860386208, 23.4540375461545), 
process = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), 
.Label = c("1", "2", "3"), 
class = "factor"), 
filtration = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), 
.Label = c("0", "1"), class = "factor"), 
psize = c(55.6444133654302, 
58.6840302266575, 54.7772459196543, 54.7604733922843, 59.3370453788836, 
51.7596253066746, 60.9248451634842, 62.9228313665229, 56.9940316793908, 
61.6950645669207), 
speed = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
.Label = c("1", "2", "3"), class = "factor")), 
.Names = c("conc", "viscos", "temp", "process", "filtration", "psize", "speed"), row.names = c("1", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9"), 
class = "data.frame")

我通过以下命令制作此数据的等高线图:

  c <- ggplot(grid, aes(x = temp, y = viscos, z = psize)) +
  stat_density_2d(aes(color=..level..), show.legend = TRUE) +
  scale_color_gradient(name = "psize", low = "#40FF00", high = "#FF0000") +
  ggtitle(title) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_dl(aes(label=..level..), method = list("bottom.pieces", cex=0.5), stat = "density_2d")

contour plot

这是我想要的图,但是,z值被重新调整为密度值。我想改变它并将“真实”z值放在图中(直接标记和图例中)。所以我想要变量psize的比例上的z值。有没有办法做到这一点?

任何帮助是极大的赞赏!

r plot ggplot2
1个回答
1
投票

你对stat_density_2d(aes(color=..level..), show.legend = TRUE)的情节有误解,它不是psize值,而是x,y坐标中点的密度分布。

由于你没有在3D中绘图,aes(z = psize)在当前的情节中没有任何意义 - 作为证明,你可以删除它并得到完全相同的情节。让我们添加geom_point来展示stat_density_2d正在展示的内容,这是积分集中的地方:

library(directlabels)

ggplot(grid, aes(x = temp, y = viscos)) +
  geom_point() +
  stat_density_2d(aes(color=..level..), show.legend = TRUE) +
  scale_color_gradient(name = "psize", low = "#40FF00", high = "#FF0000") +
  ggtitle(title) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_dl(aes(label=..level..), method = list("bottom.pieces", cex=0.5), stat = "density_2d")

你可以做的是将psize作为aes(fill=psize)添加到geom_point,它将显示psize的梯度水平,并使用geom_text包中的geom_text_repelggrepel来注释点值。

library(ggrepel)

ggplot(grid, aes(x = temp, y = viscos)) +
  geom_point(aes(fill = psize), shape=21, size=3) +
  geom_text_repel(aes(label = round(psize, digits=2)), size=3) + 
  scale_fill_gradient(name = "psize", low = "#40FF00", high = "#FF0000") +
  stat_density_2d(aes(colour=..level..), show.legend = TRUE) +
  geom_dl(aes(label=..level..), method = list("bottom.pieces", cex=0.5), stat = "density_2d") +
  scale_colour_gradient(low = "dark blue", high = "sky blue")

我使用两个梯度尺度,colourstat_density_2d(..level..)渐变和fillaes(fill=psize)渐变。如您所见,您可以在低密度点处具有高psize值(反之亦然)。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.