2017-10-11 3 views
1

myplotという既存のggplot関数を使用して、Rの複数のデータフレームからデータをプロットするループを作りたいと思います。複数のデータフレームにわたって定義されたggplot関数を持つループ

私のggplot関数はmyplotとして定義されており、抽出したいのはタイトルだけです。私は同様の投稿があることを知っていますが、既存のggplot関数の解決法はありません。ここで

df1 <- diamonds[1:30,] 
df2 <- diamonds[31:60,] 
df3 <- diamonds[61:90,] 

myplot <- ggplot(df1, aes(x = x, y = y)) + 
geom_point(color="grey") + 
labs(title = "TITLE") 

list <- c("df1","df2","df3") 
titles <- c("df1","df2","df3") 

は私の試みです:

for (i in list) { 
    myplot(plot_list[[i]]) 
    print(plot_list[[i]]) 
} 
+1

myplot、データフレームdf1、df2、df3の機能を提供する必要があります。 –

+0

投稿が更新されました – user2904120

+1

myplotとは何ですか?それはggplotの一部ではありません –

答えて

1

次のようにあなたがpredifined機能myplot()でループ内で複数のggplotsを作成することができます。

list <- c("df1","df2","df3") #just one character vector as the titles are the same as the names of the data frames 

myplot <- function(data, title){ 
    ggplot(data, aes(x = x, y = y)) + 
    geom_point(color="grey") + 
    labs(title = title) 
} 

for(i in list){ 
    print(myplot(get(i), i)) 
} 

あなた与える2つのベクトルとしたい作業した場合データフレームとタイトルの名前を次のように指定します。

list <- c("df1","df2","df3") 
titles <- c("Title 1","Plot 2","gg3") 

myplot <- function(data, title){ 
    ggplot(data, aes(x = x, y = y)) + 
    geom_point(color="grey") + 
    labs(title = title) 
} 

for(i in seq_along(list)){ #here could also be seq_along(titles) as they a re of the same length 
    print(myplot(get(list[i]), titles[i])) 
} 
+0

名前が異なると仮定して、名前のデータフレームだけでなく名前のタイトル名を得る方法 – user2904120

+1

私の答えを更新しました –

関連する問題