You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询:如何修正Dose-response curve绘图中geom_point的对应显示问题

解决剂量反应曲线中点位不匹配的问题

问题根源

你的代码同时使用了coord_trans(x="log")和scale_x_log10(),这会导致x轴被重复对数转换,直接造成数据点的x坐标偏移,无法和对应y值正确匹配。另外scale_x_log10(limits = c(2, 10000))把x轴下限设为2,导致你替换为0.5的原0剂量点被过滤;同时geom_segment里的xend=0在对数轴上无效,会引发绘图异常。

修正后的完整代码

library(drc)
library(tidyverse)

response_irgarol <- as.numeric(c("6.666667", "10", "10", "46.66667", "46.66667", "56.66667", "96.66667", "100"))
irgarol <- as.numeric(c("0", "2", "10", "20", "100", "200", "1000", "2000"))
df <- data.frame(irgarol, response_irgarol)
I <- drm(response_irgarol ~ irgarol, data = df, fct = LL.4())
newdata <- expand.grid(irgarol=exp(seq(log(0.5), log(2000), length=10000)))

pm <- predict(I, newdata = newdata, interval = "confidence")

newdata$p <- pm[,1]
newdata$pmin <- pm[,2]
newdata$pmax <- pm[,3]

df$irgarol0 <- df$irgarol
df$irgarol0[df$irgarol0 == 0] <- 0.5

ed50 <- ED(I, 50, type = "absolute", interval = "delta")
coefs <- setNames(ed50[1], "e")
y50 <- predict(I, newdata = data.frame(irgarol = ed50))

ggplot(df, aes(x=irgarol0, y=response_irgarol))+
  geom_point(shape=21, size=3, stroke=1, colour='#0081C9')+
  geom_line(data = newdata, aes(x = irgarol, y = p), lwd=1, color='#0081C9')+
  geom_segment(aes(x=coefs["e"], y=0, xend= coefs["e"], yend=y50), lty=2, lwd=0.8, colour="gray50")+
  geom_segment(aes(x=coefs["e"], y=y50, xend=0.5, yend=y50), lty=2, lwd=0.8, colour="gray50")+
  scale_x_log10(limits = c(0.5, 10000), breaks = c(0.5,2, 10, 20, round(coefs["e"]), 200, 1000, 2000), 
                labels = c("0(0.5)",2, 10, 20, round(coefs["e"]), 200, 1000, 2000))+
  xlab("Irgarol 1051 (ug/L)")+ylab("Mortality (%)")+
  theme(axis.text.x = element_text(angle = 90))+
  theme_classic()

核心调整说明

  • 移除coord_trans(x="log"):scale_x_log10()已经完成x轴的对数转换,重复转换会导致坐标偏移。
  • 调整scale_x_log10的limits为c(0.5, 10000):确保替换后的0.5剂量点能在图中显示,同时保留其他数据点的可见性。
  • 修改geom_segment的xend=0为xend=0.5:对数轴无法表示0,改为x轴实际下限值,避免绘图报错。
  • 可选优化:在x轴标签中标注0(0.5),明确原0剂量点的替换逻辑,提升图表可读性。

内容的提问来源于stack exchange,提问作者Hayoung Kwon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 00:05:25