2017-08-08 8 views
1

私は、データセットの各列に対して(quickplotを使用して)グラフを作成し、pdfとしてフォルダに保存しようとしています。ggplotを使ってRでグラフを自動的にプロットし、フォルダに保存するにはどうしたらいいですか?

これまでのところ、私はその後、私は、グラフをプロットし、保存するための関数を書くことを試みてきました

test.data <-cbind.data.frame(data$col_1,data$col_2,data$col_3) 

(私は500の以上の列でそれをしようとする前に)テストデータフレームを作りました。私は、グラフの棒グラフ(いくつかのタイトルでは&の色の指定で)の数を表示しようとしています。各カテゴリーの人々したがって、列は通常、カテゴリデータで構成されます。

plot.graphs <- function(x) {  
    for(i in colSums(x)){ 
    plots <- quickplot(i) + 
     geom_bar(color= "#6267c1", fill="#6267c1") + 
     labs(title= "i", 
      x="i", 
      y="Count") + 
     theme(help() 
     plot.title = element_text(colour = "#453694"), 
     axis.title = element_text(colour ="#453694")) 
    ggsave(plots,filename = "testplot",nm[1],".pdf",sep="") 
    print(plots) 
    } 
} 
plot.graphs(test.data) 

しかし、これは多くのエラーが発生するように思われるので、私は正しくやっているとは思わない。

+1

'(I colSumsで()' 'colSums'は関数であり、どこのために' i'が関数の内部で使用されている場合は、 'for'のための閉じ括弧のですか??別の問題は、あなたが'data.data < - data [c( "col_1"、 "col_2"、 "col_3")]' – akrun

+1

ありがとうございました。私はそれらの変更を試してみましょう! – datastudent

答えて

0

pdf()グラフィックデバイスとdev.off()でプロットコードを折り返してみてください。 pdf()は、グラフィカルデバイスを開き、dev.off()で閉じるまで、pdfグラフィカルデバイスを開き、生成したすべてのグラフィックスをファイルに保存します。

私は、データセットを持っていないので、あなたのコードをテストするが、これを試すことができません。

pdf(file = 'test.pdf', onefile = TRUE, paper = 'special', height = 11, width = 8.5) 

for(i in colSums(x)){ 
    plots <- quickplot(i) + 
     geom_bar(color= "#6267c1", fill="#6267c1") + 
     labs(title= "i", 
      x="i", 
      y="Count") + 
     theme(help() 
     plot.title = element_text(colour = "#453694"), 
     axis.title = element_text(colour ="#453694")) 
} 

dev.off() 

も参照してください:、念のために、これは誰にとっても便利ですhttps://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/pdf.html

+0

ありがとうございました!私はこれを行く:) – datastudent

0

を次このスクリプトは私のために働くことになった:

plot.auto <- function(data, list = as.list(colnames(data))){ 
     df <- data 
     ln <- length(names(data)) 
     for(i in 1:ln){ 

     plot <- quickplot(na.omit(df[,i],main=names(df)[i])) + 
      geom_bar() + 
      labs(title= colnames((df[,i])), 
       x=colnames((df)[i]), 
       y="y axis title") + 
# this makes the titles and text particular colours 
      theme(
      plot.title = element_text(colour = "#455876"), 
      axis.title = element_text(colour ="#455467"), 
# this puts the labels on an angle so they don't overlap 
      axis.text.x = element_text(angle = 30, hjust = 1))+ 
# this makes the title of the plot the same as the column name 
      ggtitle(colnames((df)[i])) + 
      geom_text(stat='count',aes(label=..count..),vjust=-0.3) 

     print(length(unique(df[,i]))) 
# this deletes any characters in the column names which will make saving 
difficult   
     save_name1<- gsub("/","or",as.character(colnames(df)[i])) 
     save_name<- gsub("\\?","",save_name1) 

#this tells you each title of the graph as the function runs  
     print(save_name) 

#this saves each graph in a folder which must be in your Working Directory 
eg. Auto_Plot_Folder (as a pdf) with the file the same as the column name 

     ggsave(plot,filename = 
paste("Auto_Plot_folder/",save_name,".pdf",sep =""),device ="pdf") 

     } 
    } 
関連する問題