R语言里一些画图程序不能在循环里正常保存的解决办法

R语言里一些画图程序不能在循环里正常保存的解决办法

  这个目前只知其然不知其所以然,但是解决问题是可以的。 Seurat里的DimPlot画图,单独使用的时候没有问题,用pdf+dev.off()可以正常输出保存图片,但是放在循环里却无法实现,原因不明。 但是解决办法也很简单,加一个print函数就可以了。示例代码如下: pdf("abc.pdf") p1<-DimPlot(mouse.data) print(p1) dev.off()   保存图片同样  
for( i in 1:length(allPathwayName)){
  this_pw <- allPathwayName[i]
  if(!(paste0(this_pw,'out.csv')%in%pwlist)){
    next
  }
  score <- read.csv(file = paste0(merge_savePath,this_pw,"out.csv"))
  score <- score[,2:5]
  colnames(score)[4] = this_pw
  expr_all_manifast_ <- merge(expr_all_manifast,score,by.x = "barcodes",by.y = "barcodes")
  
  png(filename=paste0(merge_savePath,this_pw,"_hist.png"), width=1200, height=900)
  p1 <- hist(expr_all_manifast_[,10],xlab = this_pw,main = paste0(this_pw,' score'))
  print(p1)
  dev.off()
  # create a dataset
  expr_all_manifast_$idents_res0.6 <- as.character(expr_all_manifast_$idents_res0.6)
  data <- expr_all_manifast_[which(expr_all_manifast_$idents_res0.6%in%c('7','8','9','16')),]
  #
  
  sample_lable <- paste0(data$source,'.',data$idents_res0.6)
  data <- cbind(data,sample_lable)
  
  day <- data$source
  for( i in 1:length(day)){
    if(day[i]%in%c('D0N','D0P')) day[i] = 'D0'
    if(day[i]%in%c('D2N','D2P')) day[i] = 'D2'
    if(day[i]%in%c('D4N','D4P')) day[i] = 'D4'
    if(day[i]%in%c('D7N','D7P')) day[i] = 'D7'
  }
  
  # sample size
  data <- cbind(data,day)
  day_lable <- paste0(data$day,'.',data$idents_res0.6)
  data <- cbind(data,day_lable)
  
  data <- data[-which((data$sample_lable%in%c("D0P.16","D2P.7","D7N.9","D7P.8","D7P.9","D2N.16","D2N.7"))),]
  sample_size = data %>% group_by(sample_lable) %>% summarize(num=n())
  
  fill_color <- data$source
  for(i in 1:length(fill_color)){
    if(fill_color[i]%in%c("D0N","D2N","D4N","D7N")) fill_color[i] = 'red'
    else fill_color[i] = 'blue'
  }
  jpeg(filename=paste0(merge_savePath,this_pw,"_samlable.png"), width=1500, height=500)
  
  p1 <- data %>%
    left_join(sample_size) %>%
    mutate(myaxis = paste0(sample_lable, "\n", "n=", num)) %>%
    ggplot( aes(x=myaxis, y=data[,9], fill=fill_color)) +
    geom_violin(width=1.4) +
    geom_boxplot(width=0.1, color="grey", alpha=0.2) +
    scale_fill_viridis(discrete = TRUE) +
    theme_ipsum() +
    theme(
      legend.position="none",
      plot.title = element_text(size=11)
    ) +
    ggtitle(this_pw) +
    xlab("")+
    ylab("")
  print(p1)
  dev.off()
    
  sample_size <- data %>% group_by(day_lable) %>% summarize(num=n())
  jpeg(filename=paste0(merge_savePath,this_pw,"_daylable.png"), width=1000, height=500)
  
  p1 <- data %>%
    left_join(sample_size) %>%
    mutate(myaxis = paste0(day_lable, "\n", "n=", num)) %>%
    ggplot( aes(x=myaxis, y=data[,9], fill=day)) +
    geom_violin(width=1.4) +
    geom_boxplot(width=0.1, color="grey", alpha=0.2) +
    scale_fill_viridis(discrete = TRUE) +
    theme_ipsum() +
    theme(
      legend.position="none",
      plot.title = element_text(size=11)
    ) +
    ggtitle("Propagation score") +
    xlab("")+
    ylab("")
  print(p1)
  dev.off()
    
  
}

 

上一篇:python画美国国旗,小白画法


下一篇:【程序设计思维与实践 week2 作业题】倒水问题