1

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! enter image description here enter image description here

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")
Omi
  • 69
  • 8

1 Answers1

2

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))
DPH
  • 4,244
  • 1
  • 8
  • 18
  • Thanks. How data labels like 0, 10, 100 can be seen on x-axis? – Omi Jul 17 '21 at 14:56
  • 1
    @Omi not sure I understood your task correctly: you said you wanted to squeeze the grapth towards the Y axis - I interpreted this as X axis starting at 100... can you please try to reforumlate the task (where does the x axis start and waht labels you want) thx – DPH Jul 17 '21 at 15:00
  • I want to squeeze a bit that can be done by your suggestion of scale_x_continuous(limits = c(100,3000)) but i'll use c(10, 3000). At the same time i need 10, 100 data labels to be reflected on x-axis so that the graph is more readable, Can we do this? – Omi Jul 17 '21 at 15:17
  • 1
    @Omi have a look at the altered answer, I included a second aproach that should cover your need (you could make the same edit in the first approach) – DPH Jul 17 '21 at 15:19
  • Thanks, it works now. I'm facing a small issue with my second question. I need to plot 4 graphs side by side (two of which i just pasted in the question). If we see x-axis, 2000 and 3000 overlaps is it possible to lay data labels vertically so that none of them overlaps? or some other approach to overcome the issue of overlaps? – Omi Jul 17 '21 at 15:33
  • 1
    @Omi if you refer to just rotating the x axis label you can add this line: at the end "+ theme(axis.text.x = element_text(angle = 90))".. I altered the second approach acordingly – DPH Jul 17 '21 at 15:35
  • 1
    Thanks...everything works now...much appreciated! – Omi Jul 17 '21 at 15:39