Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 绘制每个唯一类别并以特定大小保存_R_Loops_Ggplot2 - Fatal编程技术网

R 绘制每个唯一类别并以特定大小保存

R 绘制每个唯一类别并以特定大小保存,r,loops,ggplot2,R,Loops,Ggplot2,注意:过去的问题被删除了,我决定把我试图解决的整个问题都放进去,因为答案回答了我提出的问题,但没有解决问题 因此,我有一个数据帧: > 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

注意:过去的问题被删除了,我决定把我试图解决的整个问题都放进去,因为答案回答了我提出的问题,但没有解决问题

因此,我有一个数据帧:

> 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
值和
问题作为标签

ggplot代码(简化):

每个绘图都必须保存在特定文件夹中。在这个文件夹中,我们将在不同的
.png
文件(确切地说是30个
.png
文件)中拥有这30个绘图。每个文件的名称必须是其类别值(对于类别
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
文件的名称


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

这里有一种可能性:

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)
库(tidyverse)
图书馆(ggrepel)
图书馆(比例尺)
#创建保存绘图的目录

dirpath考虑通过
迭代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")    
})
by(dfz,dfz$类别,功能(子){

g@RLave这里是完整的problem@KonradRudolph这就是全部问题
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")    
})