[英]arguments imply differing number of rows with plotting row of dataframe
我有一個看起來像這樣的數據框df
a b c d
row1 1 2 2 3
row2 2 4 5 9
row3 1 4 4 6
對於每一行,我想在pdf中將直方圖寫入頁面。 我試圖這樣做
for (i in 1:nrow(df)){
histogram <- ggplot(data=df, aes(x=as.numeric(df[i,]))) +geom_histogram()
ggsave(filename="output.pdf", plot=histogram)
}
但是,這給了我錯誤arguments imply differing number of rows: 115, 734
。 從這個答案: https : //stackoverflow.com/a/26148043/651779我試圖做
df$id <- rownames(df)
df <- melt(df)
for (i in 1:nrow(df)){
histogram <- ggplot(data=df, aes(x=as.numeric(df[i,]))) +geom_histogram()
ggsave(filename="output.pdf", plot=histogram)
}
但這給了我同樣的錯誤,但是arguments imply differing number of rows: 3, 84410
不同arguments imply differing number of rows: 3, 84410
數arguments imply differing number of rows: 3, 84410
怎么樣:
for (i in 1:nrow(df)) {
df2 <- data.frame(x = as.numeric(df[i, ]))
histogram <- ggplot(data = df2, aes(x)) + geom_histogram()
ggsave(filename = "output.pdf", plot = histogram)
}
我將使用t
轉置data.frame,然后像這樣繪制:
#transpose df
df <- data.frame(t(df))
#notice aes_string in order for i to be the name as character
for (i in names(df)){
histogram <- ggplot(df, aes_string(x=i)) + geom_histogram()
ggsave(filename="output.pdf", plot=histogram)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.