Any way to get my curve plot that works in ggplot2 to work with Plotly’s ggplotly? dose-response curve – General

Since you are not providing a function, you need to provide a set of x and y points. For it to look smooth and be exact, you want to have many points. So something like this may do what you want:

library(ggplot2)
library(dr4pl)

myresponse <- data.frame("Dose"=c(16,8,4,2,1,0.5,0.25,0.125,0.062,0.031),
                         "Response"=c(98.1,95.8,91.6,90.6,89.6,80,5,22,10,-10))

myresult <- dr4pl(myresponse, dose=Dose, response=Response)


p <- ggplot(myresult$data, aes(x=Dose, y=Response))+
  geom_point() +
  scale_x_log10() +
  stat_function(fun = MeanResponse,
                args = list(theta = myresult$parameters),
                size=3)


x_high_resolution <- seq(from = min(myresponse$Dose),
                         to = max(myresponse$Dose),
                         length.out = 1000)

y_high_resolution <- MeanResponse(x_high_resolution, theta = myresult$parameters)
fitted_function <- data.frame(Dose = x_high_resolution,
                              Response = y_high_resolution)

p2 <- ggplot(mapping = aes(x=Dose, y=Response))+
  geom_point(data = myresult$data) +
  scale_x_log10() +
  geom_line(data = fitted_function, size = 3)
  

patchwork::wrap_plots(p, p2)

Created on 2022-10-24 by the reprex package (v2.0.1)

You could use geom_smooth() to reduce the number of points, but that introduces new risks, as geom_smooth() is doing a lot of computations and approximations itself. So I would rather increase the number of points and use geom_line().

Read more here: Source link