简体   繁体   English

使用过滤器或从列表创建多个图 - r

[英]Creating multiple plots with filter or from list - r

I have a data frame with 453 rows and the following columns:我有一个包含 453 行和以下列的数据框:

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.我想创建多个图,以便具有相同phrase编号的条目在同一个图中。 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.这意味着 1-7 将有一个图,8-11 将有一个图,等等。对于所有图,X 是Ratio列,Y 是Onset_N列。

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.但是,我有大量数据 - 短语从 1:60 开始。 Is there a way of creating a loop in which the filter and the title would go from 1:60?有没有办法创建一个循环,其中过滤器和标题将从 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先感谢您

You can create a function that generates your plot, say get_phrase_plot() , and then apply that function by phrase .您可以创建一个生成绘图的函数,例如get_phrase_plot() ,然后通过phrase应用该函数。 Here is an example in data.table这是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;将功能应用于每个phrase组; that I also use the special .BY to pass the phrase value as a string to be used in the title parm of labs()我还使用特殊的.BY将短语值作为字符串传递给labs()title参数
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.一种方便的方法是将参数save=F添加到函数签名中,如果将其设置为T ,则将保存绘图。

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, (ie each phrase )现在,当您调用该函数时,您可以传递save=T ,您将获得每个组的一个 .png 图(即每个phrase

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

Output is the same, but you also have the .png files saved:输出是相同的,但您还保存了 .png 文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM