【问题标题】:plot each unique category and save it with specific size绘制每个唯一类别并以特定大小保存
【发布时间】:2019-06-15 04:09:34
【问题描述】:

注意:过去的问题已被删除,我决定提出我要解决的整个问题,因为答案回答了我提出的问题但没有解决问题。

所以,我有这个数据框:

> 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 个),它们的 XY 值和 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 个图(准确地说是 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 文件的名称。

PS:绘图的保存必须使用ggsave(),因为它需要具有特定的大小。

【问题讨论】:

  • @RLave 这是完整的问题
  • @KonradRudolph 这是完整的问题

标签: r loops ggplot2


【解决方案1】:

考虑by 遍历Category 的唯一值,并使用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】:

    这是一种可能性:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多