我有以下数据,名为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")
这是我想要的图,但是,z值被重新调整为密度值。我想改变这一点,并把真正的&#39;图中的z值(直接标记和图例中)。所以我想要变量psize的比例上的z值。有没有办法做到这一点?
非常感谢任何帮助!
答案 0 :(得分: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
或{{1从geom_text_repel
包中注释点值。
ggrepel
我使用两个渐变比例,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")
的{{1}}渐变和colour
的{{1}}渐变。如您所见,您可以在低密度点处具有高stat_density_2d(..level..)
值(反之亦然)。