这是我的数据:
x
是输入值,y
是使用lm()
模型的预测值,upr
和lwr
是根据lm()
模型计算的95%置信区间
library(tidyverse)
library(scales)
library(cowplot)
library(ggpmisc)
fit_line <-dput(fit_line)
structure(list(x = c(0.048, 0.55, 0.44, 0.052, 0.029, 0.09, 0.2,
0.48, 0.51, 0.11, 1.44, 0.15, 0.4, 1.12, 0.54), y = c(535.247309812533,
1542.55243882316, 1321.82820736665, 543.273645501861, 497.122215288227,
619.523834550474, 840.248066006986, 1402.09156425993, 1462.28908192988,
659.655512997113, 3328.41212969858, 739.91886989039, 1241.56485047337,
2686.30527455236, 1522.48659959984), lwr = c(-474.582987840204,
754.520653106387, 576.903888431076, -461.460391144338, -537.324262756609,
-338.409577605306, -2.1782812716142, 647.785886082738, 696.130234140302,
-274.914182245542, 1249.20869628597, -150.884187631952, 498.439060263022,
1155.6674836111, 740.548147246478), upr = c(1545.07760746527,
2330.58422453993, 2066.75252630222, 1548.00768214806, 1531.56869333306,
1577.45724670625, 1682.67441328559, 2156.39724243711, 2228.44792971946,
1594.22520823977, 5407.61556311119, 1630.72192741273, 1984.69064068372,
4216.94306549362, 2304.4250519532)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15"))
所以我尝试绘制的是与 CI 一致的,按 log y 比例。但是发生了: 所以首先当我不记录比例时:
ggplot() +
# scale_y_log10(breaks = c(10, 100, 1000, 10000),labels = c("10", "100", "1000", "10000") ) +
coord_cartesian(xlim = c(0, 1.5),ylim = c(10, 10000)) +
theme_cowplot() +
annotation_logticks(sides = "l") +
geom_ribbon(data = fit_line, aes(x,y,ymin = lwr, ymax = upr), fill = "grey80", alpha = 0.4)
我不明白那个黑色矩形是从哪里来的? 然后当我记录 y 尺度时,我丢失了一些 CI:
ggplot() +
scale_y_log10(breaks = c(10, 100, 1000, 10000),labels = c("10", "100", "1000", "10000") ) +
coord_cartesian(xlim = c(0, 1.5),ylim = c(10, 10000)) +
theme_cowplot() +
annotation_logticks(sides = "l") +
geom_ribbon(data = fit_line, aes(x,y,ymin = lwr, ymax = upr), fill = "grey80", alpha = 0.4)
您能帮我找出代码中的问题吗?或者发生了什么?
annotation_logticks
函数从原始y轴值中获取log10,ggplot在使用scale_y_log10
对轴进行log10变换后保存该值。如果您实际上不进行转换,则 logtickes 的值将非常小(在您的情况下为 1 到 5)。这就是为什么您会看到由压缩的逻辑标记组成的黑色矩形。
正如我在评论中提到的,对于第二个图,您会丢失一些间隔,因为
lwr
中有负值。您需要决定如何处理它们。您可能想将负值转换为 -Inf。
geom_ribbon(data = fit_line %>% mutate(lwr = ifelse(lwr < 0, -Inf, lwr)),
aes(x,y,ymin = lwr, ymax = upr), fill = "grey80", alpha = 0.4)