首页 > 解决方案 > ggplot 使用 drc 包的剂量反应曲线

问题描述

我正在尝试使用以下代码使用 drc 包在 ggplot 中绘制剂量反应曲线,并有两个问题如下。 首先:我需要在 x 轴上包含 0、10、100 等并省略 4000 标签,怎么做?第二:是否可以将图形向 y 轴挤压,因为第一个数据点为 100,在此之前占用了很多空间。我需要并排排列几个图,所以如果图可以从 100 开始,以及我们如何避免标签重叠(例如下图中的 2000 和 3000)。请指导我,谢谢!在此处输入图像描述 在此处输入图像描述

gi <- as.numeric(c("0", "5.24", "24.2", 
        "37.2", "71.9", "80", 
        "100", "100", "0", 
        "0", "15.1", "42.8", "61.8", "73.5", "97.3", "100"))

conc <- as.numeric(c("0", "100", "167", "278.89", "465.74", "777.79", "1298.91", "2169.19", "0", "100", "167", "278.89", "465.74", "777.79", "1298.91", "2169.19" ))
df <- data.frame(conc, gi)
library("drc")
library(ggplot2)

Pyr <- drm(gi ~ conc, data = df, fct = LL.4(fixed = c(NA, 0, 100, NA)))
newdata <- expand.grid(conc=exp(seq(log(0.5), log(3000), length=500)))

# predictions and confidence intervals

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

# new data with predictions

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

# need to shift conc == 0 a bit up, otherwise there are problems with coord_trans

df$conc0 <- df$conc
df$conc0[df$conc0 == 0] <- 0.5

# plotting the curve

ggplot(df, aes(x = conc0, y = gi)) +
  geom_point() +
  geom_ribbon(data=newdata, aes(x=conc, y=p, ymin=pmin, ymax=pmax), alpha=0.2) +
  geom_line(data=newdata, aes(x=conc, y=p)) +
  coord_trans(x="log") +
  ggtitle("Pyridine") + xlab("Concentration (mg/l)") + ylab("Growth inhibition")

标签: rggplot2drc

解决方案


您可以在 scale_x_continuous() 函数中定义 X 轴范围:

ggplot(df, aes(x = conc0, y = gi)) +
  geom_point() +
  geom_ribbon(data=newdata, aes(x=conc, y=p, ymin=pmin, ymax=pmax), alpha=0.2) +
  geom_line(data=newdata, aes(x=conc, y=p)) +
  coord_trans(x="log") +
  # here you can decide the limits of the x-axis
  scale_x_continuous(limits = c(100,3000)) +
  ggtitle("Pyridine") + xlab("Concentration (mg/l)") + ylab("Growth inhibition")

根据您的评论:

ggplot(df, aes(x = conc0, y = gi)) +
  geom_point() +
  geom_ribbon(data=newdata, aes(x=conc, y=p, ymin=pmin, ymax=pmax), alpha=0.2) +
  geom_line(data=newdata, aes(x=conc, y=p)) +
  coord_trans(x="log") +
  # here you can decide the limits of the x-axis, breaks and labels
  scale_x_log10(limits = c(10, 3000), breaks = c(10, 100, 1000, 2000, 3000), labels = c(10, 100, 1000, 2000, 3000)) +
  ggtitle("Pyridine") + xlab("Concentration (mg/l)") + ylab("Growth inhibition") + theme(axis.text.x = element_text(angle = 90))

推荐阅读