I have a dataframe df
that looks like this
a b c d
row1 1 2 2 3
row2 2 4 5 9
row3 1 4 4 6
For each row I want to write a histogram to a page in a pdf. I tried to do it like this
for (i in 1:nrow(df)){
histogram <- ggplot(data=df, aes(x=as.numeric(df[i,]))) +geom_histogram()
ggsave(filename="output.pdf", plot=histogram)
}
However, this gives me the error arguments imply differing number of rows: 115, 734
. From this answer: https://stackoverflow.com/a/26148043/651779 I tried to do
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)
}
but this gives me the same error but with a different number arguments imply differing number of rows: 3, 84410