如何在 r 中创建多个图并将其导出为 jpeg 格式?

How to create and export multiple plots to jpeg format in r?

我一直在为 R 中的情绪分析模型的结果创建条形图。数据是来自客户的非常机密的反馈。因此,然后将反馈输入情绪分析模型以生成输出。我的工作是为每个组合生成一个图表,例如 zone = delhi 和 delhi 有像 eastdelhi、westdelhi、northdelhi、southdelhi 这样的子区域。我想生成带有组合的图表 zone = delhi 和 sub-zone = eastdelhi。我想将它保存到 jpeg file.I 已经写了一个 for 循环来做到这一点。但由于某种原因,它不起作用。这是代码

#Set locales 
rm(list = ls())
Sys.setlocale(category = "LC_ALL",locale = "English")

#Load libraries
LoadLibraries <- c("openxlsx",
                   "dplyr",
                   "tidyr",
                   "plotly",
                   "RColorBrewer",
                   "shiny",
                   "officer",
                   "parallel",
                   "dplyr",
                   "tidyr",
                   "magrittr",
                   "knitr")
lapply(LoadLibraries, require, character.only = TRUE)

path = "C:/Users/R_Visual/Data/visual_data.xlsx"
input_data <- read.xlsx(path)
name <- names(input_data[,1:10])

#Filtering the zones and circles
for (i in 1:length(unique(Zone.Final))){
  for (j in 1:length(unique(Circle.Final))){
    
    fileName = 'C:/Users/R_Visual/'+ str(i) + str(j) + '.jpeg'
    jpeg(fileName, width = 900, height = 450)
    
    df <- input_data %>% 
      filter(input_data$Zone.Final[i])
    df <- df  %>%
      filter(df$Circle.Final[j])
    
    color <- c("#ca2f27","#f56d43","#f8c38a","#fde08b","#d9ef8b","#a7d86f","#67bd64","#1a984f","#D3D3D3","#A9A9A9")
    plot <- barplot(sort(colSums(input_data[, 1:10])),
                main = paste("Sentiment Analysis for Zone",df$Zone.Final[i]," and Circle",df$Circle.Final[j], sep = ""),
                xlab = "Sentiments",
                ylab = "Count",
                horiz = FALSE,
                names = name,
                col = color,
                border = FALSE,
                legend = TRUE,
                beside = TRUE,
                legend.text = name,
                args.legend = list(bty = "n", x = "topleft",ncol = 1, cex = 0.8, y.intersp = 0.8, x.intersp = 0.25, horiz = F, xpd = TRUE, inset = c(0,0)))
dev.off()

  }
}

编辑: 这是input_data

的样本
> dput(input_data)
structure(list(anger = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), anticipation = c(1, 
0, 0, 0, 0, 0, 1, 0, 0, 0), disgust = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), fear = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), joy = c(0, 
0, 0, 0, 0, 0, 1, 0, 0, 0), sadness = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), surprise = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), trust = c(0, 
0, 1, 1, 1, 0, 2, 0, 0, 0), negative = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), positive = c(1, 0, 0, 0, 1, 1, 2, 1, 0, 1), Zone.Final = c("Delhi", 
"Lucknow", "Durgapur", "Lucknow", "Mumbai", "Bhopal", "Chandigarh", 
"Chandigarh", "Gurugram", "Chandigarh"), Circle.Final = c("Noida", 
"Gorakhpur", "Murshidabad", "Gorakhpur", "Mumbai City", "Bhopal", 
"Chandigarh", "Panchkula", "Hisar", "Karnal")), row.names = c(NA, 
10L), class = "data.frame")

如果有人能帮我写代码,那会很有帮助。

您可以尝试创建一个组合区域和子区域的列表:

#Data
input_data <- structure(list(anger = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), anticipation = c(1, 
0, 0, 0, 0, 0, 1, 0, 0, 0), disgust = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), fear = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), joy = c(0, 
0, 0, 0, 0, 0, 1, 0, 0, 0), sadness = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), surprise = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), trust = c(0, 
0, 1, 1, 1, 0, 2, 0, 0, 0), negative = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), positive = c(1, 0, 0, 0, 1, 1, 2, 1, 0, 1), Zone.Final = c("Delhi", 
"Lucknow", "Durgapur", "Lucknow", "Mumbai", "Bhopal", "Chandigarh", 
"Chandigarh", "Gurugram", "Chandigarh"), Circle.Final = c("Noida", 
"Gorakhpur", "Murshidabad", "Gorakhpur", "Mumbai City", "Bhopal", 
"Chandigarh", "Panchkula", "Hisar", "Karnal")), row.names = c(NA, 
10L), class = "data.frame")

#Code
#First create and global id to combine zone and subzone
df <- input_data
df$id <- paste(df$Zone.Final,df$Circle.Final,sep = '-')
#Split
List <- split(df,df$id)
#Plot
color <- c("#ca2f27","#f56d43","#f8c38a","#fde08b","#d9ef8b","#a7d86f","#67bd64","#1a984f","#D3D3D3","#A9A9A9")
#Plot names
vnames <- paste0(names(List),'.jpeg')
#Loop
for(i in 1:length(List))
{
  name <- names(List[[i]][, 1:10])
  #Plot
  jpeg(filename = vnames[i], width = 900, height = 450)
  barplot(sort(colSums(List[[i]][, 1:10])),
          main = paste("Sentiment Analysis for Zone ",
                       unique(List[[i]]$Zone.Final),
                       " and Circle ",unique(List[[i]]$Circle.Final), sep = ""),
          xlab = "Sentiments",
          ylab = "Count",
          horiz = FALSE,
          names = name,
          col = color,
          border = FALSE,
          legend = TRUE,
          beside = TRUE,
          legend.text = name,
          args.legend = list(bty = "n", x = "topleft",ncol = 1,
                             cex = 0.8, y.intersp = 0.8, x.intersp = 0.25,
                             horiz = F, xpd = TRUE, inset = c(0,0)))
  dev.off()
}

这将创建地块。当然,您可以将路径添加到 vnames,例如您必须在该文件夹中保存绘图的目录。