简体   繁体   English

R:如何将函数应用于数据框以使用唯一因子组合绘制每个子集的图

[英]R: How to apply a function to a data frame to make plots of each subset with a unique factor combination

I would like to understand how the apply functions or anything similar can be used to make plots for several subsets of a data frame. 我想了解应用函数或类似函数如何用于为数据框的几个子集绘制图。 Let's assume I want to use the mtcars data frame and I want to make a plot for each combination of "gear" and "carb". 让我们假设我想使用mtcars数据框,我想为“齿轮”和“碳水化合物”的每个组合制作一个图。

I read here , that one can just write a function and then use lapply to create subsets of a data frame and plots for each of them. 在这里读到,人们可以编写一个函数,然后使用lapply创建数据框的子集并绘制每个子集的图。 So how could I rewrite this to use lapply or anything similar? 那么我怎么能重写这个来使用lapply或类似的呢?

gg_test = function(data) {
    title = paste("gear",data[,"gear"][1],"carb",data[,"carb"][1])

    require(ggplot2)
    gg1 = ggplot(data, aes(x = mpg, y = cyl, color = disp)) + geom_point() +
        ggtitle(title)
    print(gg1)
} 

for (g in unique(mtcars[,"gear"])){
    for(ca in unique(mtcars[,"carb"])){
        df_sub = subset(mtcars, gear == g & carb == ca)
        gg_test(df_sub)
    }
}

You can use split and interaction to divide a data.frame into chunks that you can then apply your plot function to: 您可以使用splitinteraction将data.frame划分为块,然后可以将绘图函数应用于:

gg_test = function(data) {
  title = paste("gear",data[,"gear"][1],"carb",data[,"carb"][1])

  require(ggplot2)
  ggplot(data, aes(x = mpg, y = cyl, color = disp)) + geom_point() +
    ggtitle(title)
} 

I modified your plot function to return the plot instead of print it out. 我修改了你的绘图功能以返回绘图而不是打印出来。

plots <- lapply(split(mtcars, interaction(mtcars[,c("gear","carb")]), drop = TRUE), gg_test)

Then I store each plot in another object that can be printed on demand. 然后我将每个绘图存储在另一个可以按需打印的对象中。

暂无
暂无

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

相关问题 R:按因子将函数应用于数据帧的每一行,在函数中调用一个值(按因子) - R: apply function to each row of a data frame by factor, calling in the function a value (by factor) 如何按因子对数据帧进行子集化,并为每个子集重复绘制图? - How subset a data frame by a factor and repeat a plot for each subset? 如何在 r 中编写一个函数来绘制每个唯一值的数据? - How to write a function in r which plots data for each unique value? 如何对数据帧进行子集处理,如何将函数与for循环一起使用,并获得带有每个子集名称的结果? - How to subset a data frame, apply a function with a for-loop and obtain the result with the name of each subset on it? R:拆分数据帧,将 function 应用于每个子集中的所有行对 - R: split a data-frame, apply a function to all row-pairs in each subset 将自定义函数应用于数据框的每个子集并生成数据帧 - Apply custom function to each subset of a data frame and result a dataframe 按行列出data.frame并按行在每个部分上应用函数 - Subset a data.frame by list and apply function on each part, by rows 如何使用ddply将函数应用于按数据帧的另一列中的每个因子划分的一列? - How do I apply a function to one column split by each factor in another column of a data frame using ddply? 如何重新排序data.frame子集的因数并将其应用于主data.frame? - How to reorder factor of a subset of data.frame and apply it to main data.frame? 如何对每个唯一因子值使用一次应用功能 - How to use apply function once for each unique factor value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM