代码之家  ›  专栏  ›  技术社区  ›  Chris

绘制每个唯一类别,并按特定大小保存

  •  0
  • Chris  · 技术社区  · 7 年前

    注意:过去的问题被删除了,我决定把我要解决的整个问题,因为答案与我问的问题一致,但没有解决问题。

    所以,我有一个数据框架:

    > head(dfz)
                X    Y   Question  Category
    1        1.00 0.32         Q1       FIN
    5        0.27 0.61         Q2       IKA
    6        0.13 0.39         Q3       OKS
    7        0.25 0.60         Q4       RES
    9        0.09 0.57         Q5       RES
    12       0.04 0.39         Q6       IKA
    

    我需要每个类别的ggplot(它们是30个独特的类别,但你可以看到上面的4个)和它们的 X Y 价值观与 Question 作为标签。

    ggplot代码(简化):

    ggplot(FIN, aes(x=X, y=Y)) + 
        geom_point(colour="red",size=3) +
        geom_text_repel(label=df$Question, family="sans", fontface="bold", size=4) +
        scale_x_continuous(labels = scales::percent_format(accuracy = 1)) + 
        scale_y_continuous(labels = scales::percent_format(accuracy = 1), position = "right") 
    

    每个绘图都必须保存在特定的文件夹中。在这个文件夹中,我们将在不同的 .png 文件(30) PNG 文件要精确)。每个文件的名称必须是其类别值(对于类别 FIN 文件应该是 "C:/ME/Plots/FIN.png" ,为了 IKA 应该是 "C:/ME/Plots/IKA.png" 等)。

    预期输出如下:

    [1] "C:/ME/Plots/FIN.png " # Plot of FIN category
    [1] "C:/ME/Plots/PLE.png " # Plot of IKA category
    [1] "C:/ME/Plots/OKS.png " # Plot of OKS category
    [1] "C:/ME/Plots/INX.png " # Plot of RES category
    [1] "C:/ME/Plots/MES.png " # Plot of PLZ category
    

    因此,我决定采取下一种方法: (1)创建30个数据帧,(2)创建30个数据帧的列表,(3)遍历该数据帧列表,(4)绘制每个循环,(5)将每个循环的绘图保存在特定文件夹中。但是,每次循环工作时,我都在努力调用数据帧的“名称”(这个问题在过去的问题中解决了,但不是整个问题,所以我们决定删除它并提出一个新问题)。

    一个更简单的方法可能是(但我不知道如何实现它): (1)告诉ggplot()为每个类别打印一个图,(2)用 ggsave() 在特定文件夹中,类别名称作为 PNG 文件。

    附:保存情节必须与 GGSAVER() 因为它需要一个特定的大小。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Parfait    7 年前

    考虑 by 迭代 类别 并使用将每个子集数据图保存到磁盘 ggsave :

    by(dfz, dfz$Category, function(sub) {
       g <- ggplot(sub, aes(x=X, y=Y)) + 
               geom_point(colour="red",size=3) +
               geom_text_repel(label=sub$Question, family="sans", fontface="bold", size=4) +
               scale_x_continuous(labels = scales::percent_format(accuracy = 1)) + 
               scale_y_continuous(labels = scales::percent_format(accuracy = 1), position = "right")
    
       fn <- paste0("C:/ME/Plots/", sub$Category[1], ".png")
    
       ggsave(file=fn, plot=g, device = "png")    
    })
    
        2
  •  0
  •   meriops    7 年前

    有一种可能性:

    library(tidyverse)
    library(ggrepel)
    library(scales)
    
    # create directory where to save plots
    dirpath <- file.path("C:", "ME", "plots")
    
    if (!dir.exists(dirpath)) {
      dir.create(dirpath, showWarnings = F, recursive = T)
    }
    
    # define plotting function
    fff <- function(d, dirpath) {
      #plot
      p <-
        ggplot(d, aes(x=X, y=Y)) + 
        geom_point(colour="red",size=3) +
        geom_text_repel(aes(label=Question), 
                        family="sans",
                        fontface="bold", 
                        size=4) +
        scale_x_continuous(labels = percent_format(accuracy = 1)) + 
        scale_y_continuous(labels = percent_format(accuracy = 1),
                           position = "right") 
      #save
      fn <- paste0(as.character(d$Category[1]), ".png")
      fp <- file.path(dirpath, fn) 
      ggsave(filename = fp, plot = p)
    }
    
    # split data to list by Category
    dfz_sp <- split(dfz, dfz$Category)
    
    # save the plots
    dfz_sp %>%
      walk(fff, dirpath)
    
    推荐文章