The best way to do something like this, (IMO) would be using ggplot2
, rather than with()
and plot()
. The main gist of the difference (in a psuedo-code kinda way) is that with()
would look like this:
with(predict(make_model(y~x,Data)),plot(...))
But ggplot()
and tidyverse
would look like this:
Data %>%
make_model(y ~ x)
predict() %>%
ggplot() +
geom_*(...)
The ggplot
version (to me at least) looks a bit tidier. There are less surrounding brackets and the code flows easier. This is big thing amongst R users (base vs tidy), but I say use whichever makes your code easier for others to read.
So, you want to plot the main estimate from your data and a confidence band, I'd recommend using the geom_ribbon()
function for the confidence interval and then when you get better at ggplot
stuff, exploring the different themes available
I'll use the dosresmeta()
function from Shim et al for making the model. I'll also say that this paper is definitely aimed at teaching the dose-response methods much more than teaching R because the way it teaches R is (quite frankly) appalling. If you're wanting to learn R from this, I'd suggest finding a much better resource.
library(tidyverse)
read.csv(ādrma_bin.csvā, header=TRUE) %>%
dosresmeta(logrr ~ dose, id=id, type=type, se=se, cases=cases, n=n, data=.) %>%
predict() %>%
ggplot() + #This starts a new canvas with your data as the main source
geom_line(aes(dose,pred)) + #draw a line for your main estimate
geom_ribbon(aes(ymin = ci.lb, ymax= ci.ub))# this draws your 95% CI
In base R (without the tidyverse
stuff), this would best be written as:
data_bin <- read.csv(ādrma_bin.csvā, header=TRUE)
model <- dosresmeta(logrr ~ dose, id=id, type=type, se=se, cases=cases, n=n,data = data_bin)
pred <- predict(model)
plot(pred$dose, pred$pred, type="l")
lines(pred$dose,pred$ci.lb)
lines(pred$dose,pred$ci.ub)
or using with()
as suggested by Shim et al:
data_bin <- read.csv(ādrma_bin.csvā, header=TRUE)
model <- dosresmeta(logrr ~ dose, id=id, type=type, se=se, cases=cases, n=n,data = data_bin)
with(
predict(model),
{
plot(dose, pred);
lines(dose, ci.lb);
lines(dos, ci.ub)
})
This is of course assuming you get the data out of the supp. material they provided which is just more effort than it should be. Learn R elsewhere, this paper is not useful for learning it.
Edit:
Also, if you ever need to know something about a function, almost all functions in R have their own help page. You can access it using ?
so run ?with
and it will pop up with a help file. This is much easier if using RStudio since it organises the windows in panes for you.