Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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中创建多个绘图并将其导出为jpeg格式?_R_For Loop_Plot_Bar Chart_Jpeg - Fatal编程技术网

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

如何在r中创建多个绘图并将其导出为jpeg格式?,r,for-loop,plot,bar-chart,jpeg,R,For Loop,Plot,Bar Chart,Jpeg,我一直在为R中的情绪分析模型的结果创建条形图。数据是来自客户的非常机密的反馈。因此,反馈随后被反馈到情绪分析模型中以生成输出。我的工作是为每个组合生成一个图表,例如zone=德里,德里有东德里、西德里、北德里、南德里等分区。我想生成具有如下组合的图表 分区=德里,分区=东德里。我想把它保存到一个jpeg文件中,为此我写了一个for循环。但由于某种原因,它不起作用。这是密码 #Set locales rm(list = ls()) Sys.setlocale(category = "L

我一直在为R中的情绪分析模型的结果创建条形图。数据是来自客户的非常机密的反馈。因此,反馈随后被反馈到情绪分析模型中以生成输出。我的工作是为每个组合生成一个图表,例如zone=德里,德里有东德里、西德里、北德里、南德里等分区。我想生成具有如下组合的图表 分区=德里,分区=东德里。我想把它保存到一个jpeg文件中,为此我写了一个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()

  }
}

如果有人能帮我编写代码,那将非常有帮助。

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

#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()
}
#数据

输入数据请
dput(输入数据)
复制输出并添加到问题中以帮助您@Duck,我添加了一个数据示例。你能帮我解决这个问题吗?我已经为你的问题添加了一个可能的解决方案!请检查!
#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()
}