0

For a very large table, I would like to automatically have a barplot for each column, which is then also saved as a png.

The title can simply be the column title and the description of the columns correspond to the column variables. So no individual edit of the barplots is necessary

I have already created barplots by hand and experimented with the "lapply" command without success. Here the barplot code

png(file="a_x.png", width=600, height=400)
barplot(table(Example$a_x), main = "a_x")
dev.off()

enter image description here

jpsmith
  • 11,023
  • 5
  • 15
  • 36
Dnjl_01
  • 27
  • 3
  • 1
    Data [as plain text](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) please. We cannot read data into R from images. – neilfws Feb 17 '23 at 11:00
  • Does this answer your question? [ggplot2 - create a barplot for every column of a dataframe](https://stackoverflow.com/questions/52822840/ggplot2-create-a-barplot-for-every-column-of-a-dataframe) – Marco Feb 17 '23 at 11:01

1 Answers1

1

You can do a simple for loop and paste0 for the file name:

Data

df <- data.frame(a_x = sample(c("Yes","No"), 100, prob = c(0.10,0.90), replace = TRUE),
                 b_x = sample(c("Yes","No"), 100, prob = c(0.10,0.90), replace = TRUE),
                 c_x = sample(c("Yes","No"), 100, prob = c(0.10,0.90), replace = TRUE))

Code

for(i in colnames(df)){
  png(file = paste0(i,".png"), width = 600, height = 400)
  barplot(table(df[i]), main = i)
  dev.off()
}
jpsmith
  • 11,023
  • 5
  • 15
  • 36