gpt4 book ai didi

r - 在 geom_abline 中跨面使用不同的斜率

转载 作者:行者123 更新时间:2023-12-01 12:20:38 24 4
gpt4 key购买 nike

这是我的数据集的结构:

> dput(data)
structure(list(es = c(0.29603085763985, 0.421393627439682, 0.189653473156549,
0.226685054608428, 0.291373762079697, 0.166533544378467, 0.250586529054368,
0.146320008054403, 0.199565119644333, -0.0819047677231083, 0.15963948187092,
-0.154628141843561, 0.201121044198443, 0.0867981239977565, 0.543870310978598,
0.34547921143505, 0.37557241352574, -0.287318919407836, 0.207937483228907,
0.190143660810163, 0.276182673435993, 0.128596803172119, 0.454753165843559,
0.399237234440439, 0.32075358541748, 0.362664873575803, -0.0865925288159671,
0.51290512543514, 0.186308318839249, 0.147936083867325, 0.243792477087184,
0.625169403695832, 0.110317782120045, 0.217836235313289, 0.171468156841181,
0.50548821117127, 0.164418265301427, -0.00246305543239786, 0.325552346507191,
0.381240606108843, 0.19337350462531, 0.0408803528990759, 0.321815078821239,
0.307642815014319, 0.29603085763985, 0.421393627439682, 0.189653473156549,
0.226685054608428, 0.291373762079697, 0.166533544378467, 0.250586529054368,
0.146320008054403, 0.199565119644333, -0.0819047677231083, 0.15963948187092,
-0.154628141843561, 0.201121044198443, 0.0867981239977565,
0.543870310978598,
0.34547921143505, 0.37557241352574, -0.287318919407836, 0.207937483228907,
0.190143660810163, 0.276182673435993, 0.128596803172119, 0.454753165843559,
0.399237234440439, 0.32075358541748, 0.362664873575803, -0.0865925288159671,
0.51290512543514, 0.186308318839249, 0.147936083867325, 0.243792477087184,
0.625169403695832, 0.110317782120045, 0.217836235313289, 0.171468156841181,
0.50548821117127, 0.164418265301427, -0.00246305543239786, 0.325552346507191,
0.381240606108843, 0.19337350462531, 0.0408803528990759, 0.321815078821239,
0.307642815014319, 0.29603085763985, 0.421393627439682, 0.189653473156549,
0.226685054608428, 0.291373762079697, 0.166533544378467, 0.250586529054368,
0.146320008054403, 0.199565119644333, -0.0819047677231083, 0.15963948187092,
-0.154628141843561, 0.201121044198443, 0.0867981239977565, 0.543870310978598,
0.34547921143505, 0.37557241352574, -0.287318919407836, 0.207937483228907,
0.190143660810163, 0.276182673435993, 0.128596803172119, 0.454753165843559,
0.399237234440439, 0.32075358541748, 0.362664873575803,
-0.0865925288159671,
0.51290512543514, 0.186308318839249, 0.147936083867325, 0.243792477087184,
0.625169403695832, 0.110317782120045, 0.217836235313289, 0.171468156841181,
0.50548821117127, 0.164418265301427, -0.00246305543239786, 0.325552346507191,
0.381240606108843, 0.19337350462531, 0.0408803528990759, 0.321815078821239,
0.307642815014319), MAT = c(4.9, 4.9, 15.5, 14.1, 14.1, 14.1,
11.5, 11.5, 11.5, 17, 6.1, 2.7, 2.2, 2.2, 14.1, 14.1, 14.1, 9.5,
9.5, 9.5, 9.5, 9.3, 8.3, 8.266666651, 8.266666651, 4.3, 4.3,
22.3, 14.1, 14.1, 14.1, 8.5, 8.5, 8.5, 8.5, 21.5, 21.5, 3.8,
3.8, 6, 6, 6, 6, 6, 4.9, 4.9, 15.5, 14.1, 14.1, 14.1, 11.5, 11.5,
11.5, 17, 6.1, 2.7, 2.2, 2.2, 14.1, 14.1, 14.1, 9.5, 9.5, 9.5,
9.5, 9.3, 8.3, 8.266666651, 8.266666651, 4.3, 4.3, 22.3, 14.1,
14.1, 14.1, 8.5, 8.5, 8.5, 8.5, 21.5, 21.5, 3.8, 3.8, 6, 6, 6,
6, 6, 4.9, 4.9, 15.5, 14.1, 14.1, 14.1, 11.5, 11.5, 11.5, 17,
6.1, 2.7, 2.2, 2.2, 14.1, 14.1, 14.1, 9.5, 9.5, 9.5, 9.5, 9.3,
8.3, 8.266666651, 8.266666651, 4.3, 4.3, 22.3, 14.1, 14.1, 14.1,
8.5, 8.5, 8.5, 8.5, 21.5, 21.5, 3.8, 3.8, 6, 6, 6, 6, 6), CO2dif = c(162L,
162L, 190L, 165L, 165L, 165L, 200L, 200L, 200L, 150L, 335L, 335L,
335L, 335L, 348L, 348L, 348L, 200L, 200L, 200L, 200L, 220L, 350L,
350L, 350L, 350L, 350L, 350L, 180L, 180L, 180L, 130L, 130L, 130L,
130L, 320L, 320L, 360L, 360L, 345L, 345L, 350L, 348L, 348L, 162L,
162L, 190L, 165L, 165L, 165L, 200L, 200L, 200L, 150L, 335L, 335L,
335L, 335L, 348L, 348L, 348L, 200L, 200L, 200L, 200L, 220L, 350L,
350L, 350L, 350L, 350L, 350L, 180L, 180L, 180L, 130L, 130L, 130L,
130L, 320L, 320L, 360L, 360L, 345L, 345L, 350L, 348L, 348L, 162L,
162L, 190L, 165L, 165L, 165L, 200L, 200L, 200L, 150L, 335L, 335L,
335L, 335L, 348L, 348L, 348L, 200L, 200L, 200L, 200L, 220L, 350L,
350L, 350L, 350L, 350L, 350L, 180L, 180L, 180L, 130L, 130L, 130L,
130L, 320L, 320L, 360L, 360L, 345L, 345L, 350L, 348L, 348L)), row.names = c(NA,
-132L), class = "data.frame", .Names = c("es", "MAT", "CO2dif"
))

我想创建一个分面 ggplot,其中每个分面都包含背景中的相同点,但每个分面都有一条线,分面之间的截距不同。换句话说,各个面的斜率都相同,但截距不同。

首先,我将数据复制 number_of_facets 次,在本例中为 3 次,因为每个面绘制相同的点。每个数据集沿新变量 precplot 都有不同的值:500、1000 或 1500:

data1 <- data; data1$precplot <- 500
data2 <- data; data2$precplot <- 1000
data3 <- data; data3$precplot <- 1500

加入三个数据集

dataplot <- full_join(data1, data2)
dataplot <- full_join(dataplot, data3)

现在剧情:

ggplot(dataplot, aes(x = MAT, y = es, color = CO2dif)) + 
geom_point(size = 3) + scale_color_gradient(low = "green", high = "red") +
geom_abline(aes(intercept = -0.1846 + 0.0002 * precplot), slope = 0.0211, color = "red", size = 3) +
facet_wrap(~precplot)

请注意,geom_abline 中的intercept 取决于precplot 的值,但是,生成的绘图显示与 intercept=0 相同的线跨越三个方面。

为什么 ggplot 没有按照我的预期计算截距? enter image description here

最佳答案

您所要做的就是在美学命令中移动 slope。这是解决方案:

ggplot(dataplot, aes(x = MAT, y = es, color = CO2dif)) + 
geom_point(size = 3) + scale_color_gradient(low = "green", high = "red") +
geom_abline(aes(intercept = -0.1846 + 0.0002 * precplot, slope = 0.0211), color = "red", size = 3) +
facet_wrap(~precplot)

enter image description here

关于r - 在 geom_abline 中跨面使用不同的斜率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44461904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com