Home > front end >  Plot multiple line graphs on one graph
Plot multiple line graphs on one graph

Time:04-20

I can get a single power curve shown below but I want to create a power analysis graph. I want to change my delta value (to .6, .7, and .8) and plot those 3 other lines on that same r curve in a different color. I provided an example of what I kinda want it to look.

n_participants <- c(5, 10, 20, 30, 40)

npercluster <- 20

n_tot <- n_participants*npercluster

icc <- 0.6  # assumption

deff <- 1   icc*(npercluster - 1)

ess <- n_tot / deff

mydelt <- 0.5  

mypowers <- power.t.test(n=ess, delta=mydelt)$power

plot(n_participants, mypowers, type='l', 
     main=paste('Power based on', npercluster, 'volumes per participants'), 
     xlab='Number of participants', ylim=c( 0, 1), 
     ylab='Power')

enter image description here

enter image description here

CodePudding user response:

If you are planning to use R a lot I would recommend investing in learning ggplot2. Base R plotting solutions get very limited very quickly.

To solve your problem I would make a data frame with every combination of effect size and sample size.

dat <- expand.grid(mydelt=c(0.5,0.6,0.7,0.8), ess=n_tot / deff)

Then add a column for the power:

dat$mypowers = power.t.test(n=dat$ess, delta=dat$mydelt)$power

Then I can use ggplot to easily make a nice graph of the power curves:

library(ggplot2)
ggplot(dat, aes(x=ess, y=mypowers, color=factor(mydelt)))   geom_point()   geom_line()

enter image description here

You can easily change the overall graph look and add appropriate labels:

ggplot(dat, aes(x=ess, y=mypowers, color=factor(mydelt)))   
  geom_point()   
  geom_line()   
  theme_bw()   
  labs(x="Effective sample size", y="Power", color="Effect size" )

enter image description here

In response to the comment.. there was a mistake in the code above in that I plotted the effective total sample size on the x axis not the sample size per cluster. So instead we should make sure we have n_participants in the dataset for plotting, then calculate the powers and plot:

So the whole script is now:

n_participants <- 5:40
npercluster <- 20
icc <- 0.6  # assumption
deff <- 1   icc*(npercluster - 1)

dat <- expand.grid(mydelt=c(0.5,0.6,0.7,0.8), npart=n_participants)
dat$n_tot <- dat$npart*npercluster
dat$ess <- dat$n_tot / deff
dat$mypowers <- power.t.test(n=dat$ess, delta=dat$mydelt)$power

library(ggplot2)
ggplot(dat, aes(x=npart, y=mypowers, color=factor(mydelt)))   
  geom_line() 
  theme_bw()   
  labs(x="Number of participants", y="Power", color="Effect size" )

Which gives this graph:

enter image description here

CodePudding user response:

You may put the logic in a function f, sapply over desired deltas and - as also suggested in comments - use matplot without having to bother with any new packages.

f <- \(mydelt=.5, n_participants=c(5, 10, 20, 30, 40), npercluster=20, icc=.6) {
  n_tot <- n_participants*npercluster
  deff <- 1   icc*(npercluster - 1)
  ess <- n_tot/deff
  power.t.test(n=ess, delta=mydelt)$power
}

deltas <- seq(.5, .8, .1)

res <- t(sapply(deltas, f))

matplot(res, type='l', main=paste('Power based on 20 volumes per participants'), 
        xlab='Number of participants', 
        ylab='Power')

legend('topleft', legend=deltas, col=seq_along(deltas), lty=seq_along(deltas), 
       title='delta', cex=.8)

enter image description here

It's also possible pipe it directly into matplot:

t(sapply(deltas, f)) |> 
  matplot(res, ...)

See ?matplot for easy customizing of colors, linetypes etc.

Note: R >= 4.1 used.

  • Related