Home > Enterprise >  Creating multiple plots with filter or from list - r
Creating multiple plots with filter or from list - r

Time:07-22

I have a data frame with 453 rows and the following columns:

library(data.table)
library(dplyr)
library(ggplot2) 

    phrase onsets   IOI   Per Ratio PerMax Onset_N
       <dbl>  <dbl> <dbl> <dbl> <chr>  <dbl>   <dbl>
1          1   106. 0.413   266 1.55    3.06       1
2          1   106. 0.413   266 0.51    3.06       2
3          1   106. 0.136   266 0.86    3.06       3
4          1   106. 0.23    266 1.12    3.06       4
5          1   107. 0.299   266 1.89    3.06       5
6          1   107. 0.503   266 0       3.06       6
7          1   107. 0.503   266 0       3.06       6
8          2   108. 0.517   162 0.99    8.73       1
9          2   108. 0.161   162 0.92    8.73       2
10         2   108. 0.149   162 0.94    8.73       3
11         2   108. 0.152   162 1.12    8.73       4

I want to create multiple plots so that entries with the same phrase number are in the same plot. This means that there would be one plot for 1-7, one plot for 8-11, etc. For all plots, X is the Ratio column and Y is the Onset_N column.

This is an example of one plot:

plot1 <- data %>%
filter(phrase == 1)%>%
    ggplot(aes(Onset_N, Ratio)) 
  geom_point() 
labs(x = "onsets", y = "Ratio onset vs. per", title = "1")
plot1

However, I have a large amount of data - phrases go from 1:60. Is there a way of creating a loop in which the filter and the title would go from 1:60?

Another path would be to create a list

listphrases <- split(data, data$phrase)
listphrases <- data.table(listphrases)

But then, I'm stuck on how to plot the info that I need from the list.

Any suggestions? And of course, saving all the images with one code would also be handy.

Thank you in advance

CodePudding user response:

You can create a function that generates your plot, say get_phrase_plot(), and then apply that function by phrase. Here is an example in data.table

  1. Make function
get_phrase_plot <- function(dt,phrase) {
  ggplot(dt,aes(Onset_N, Ratio)) 
    geom_point() 
    labs(x = "onsets", y = "Ratio onset vs. per", title = phrase)
}
  1. Apply function to each phrase group; that I also use the special .BY to pass the phrase value as a string to be used in the title parm of labs()
setDT(data)
data[,.(plot = list(get_phrase_plot(.SD, .BY))), phrase]

Output is a data.table of plots

   phrase    plot
    <int>  <list>
1:      1 <gg[9]>
2:      2 <gg[9]>

There are lots of ways to save these plots; one convenient way would be to add a param save=F to the function signature, where if this is set to T, the plots would be saved.

get_phrase_plot <- function(dt,phrase, save=F) {
  plt = ggplot(dt,aes(Onset_N, Ratio)) 
    geom_point() 
    labs(x = "onsets", y = "Ratio onset vs. per", title = phrase)
  if(save) {
    ggsave(filename=paste0("plot_",phrase,".png"), height=8, width=14)
  }
  plt
}

Now, when you call the function, you can pass save=T, and you will get one .png plot for each group, (i.e. each phrase)

data[,.(plot = list(get_phrase_plot(.SD, .BY, save=T))), phrase]

Output is the same, but you also have the .png files saved:

> list.files(pattern="plot_\\d")
[1] "plot_1.png" "plot_2.png"
  • Related