问题描述
我正在尝试使用 drc 包使用以下代码在 ggplot 中绘制剂量反应曲线,并有以下两个问题。
第一:我需要包括 0、10、100 等并省略 x 轴上的 4000 标签,怎么做?。 第二:是否可以将图形向y轴挤压,因为第一个数据点是100,在此之前占用了很多空间。我需要并排排列几个图,如果图可以从 100 开始,以及我们如何避免标签重叠(例如下图中的 2000 和 3000)。请指导我这个,谢谢!
gi <- as.numeric(c("0","5.24","24.2","37.2","71.9","80","100","0","15.1","42.8","61.8","73.5","97.3","100"))
conc <- as.numeric(c("0","167","278.89","465.74","777.79","1298.91","2169.19","2169.19" ))
df <- data.frame(conc,gi)
library("drc")
library(ggplot2)
Pyr <- drm(gi ~ conc,data = df,fct = LL.4(fixed = c(NA,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,y=p)) +
coord_trans(x="log") +
ggtitle("Pyridine") + xlab("Concentration (mg/l)") + ylab("Growth inhibition")
解决方法
您可以在 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,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,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,labels = c(10,3000)) +
ggtitle("Pyridine") + xlab("Concentration (mg/l)") + ylab("Growth inhibition") + theme(axis.text.x = element_text(angle = 90))