I'm trying to plot dose-response curve in ggplot using drc package using below code and have two questions as follows. First: I need to include 0, 10, 100 etc and omit 4000 label on the x axis, how it can be done?. Second: Is it possible to squeeze the graph towards y-axis as the first data point is at 100, much space is taken up before that. I need to arrange several plots side by side so if the plot can start from 100 and how we can avoid the overlap of labels (for example 2000 and 3000 in the image below). Please guide me with this, thanks!
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")
you can define the X-axis limits within the scale_x_continuous() function:
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")
acording to your comment:
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))