0

I am calculating descriptive statistics for my dataset, using R, wich I am not very familiar to. My dataset comprises of categorical variables and I would like to create bar charts with the counts of each value's frequencies. I was trying to use a for loop to create all the frequency plots concurrently ( 'multi.hist' does not apply in my case, since it requires a numeric matrix, I tried to use it but it resulyed to an error: 'x' must be numeric ).

I am trying to built a loop in which a ggplot plot will be created for every variable/column. I have been using the following code:

library(ggplot2)

for (x in c(1:ncol(mydata.f))) {ggplot(data.frame(mydata.f$x), aes(x=mydata.f$x)) +  geom_bar()

}

Instead of the plots, when I ran the code nothing happens.

Anna
  • 177
  • 13
  • What exactly does "all the frequency plots" mean? What exactly do you want in each of these plots? Normally a ggplot will have a call to some `geom_*` function. It's not clear to me exactly what you are trying to do here. – MrFlick Jun 10 '19 at 15:22
  • As @MrFlick says, it doesn't look like you're using ggplot correctly. One additional point, when you've got that nailed down, inside a loop you'll need `print(ggplot())` to get output. – eac2222 Jun 10 '19 at 15:25
  • @MrFlick Hi, thanks very much for your answer.I am trying to create graphs with the frequencies of the levels of each variable in my dataset. But since there is a high number of variables (all categorical) I am trying to use a for loop to create a graph for each column/variable. – Anna Jun 10 '19 at 15:28
  • 1
    ggplot by itself won't do anyhting, you need to tell it what to with the data, so use geom_line (line plots), geom_point (dot plots) etc..this is what @MrFlick was referring to say by saying geom_*. Also read this chapter for more guidance https://r4ds.had.co.nz/data-visualisation.html – infominer Jun 10 '19 at 15:29
  • what kind of graph are you looking for? maybe reshape the data to do facets(panels) for each variable - many panels to get one graph with multiple panels. – infominer Jun 10 '19 at 15:30
  • @infominer thank you for your answer and the suggested reading material. I am trying to create bar charts. – Anna Jun 10 '19 at 15:34
  • @eac2222 Thanks for answering, I will include it when I sort the rest of the code out. – Anna Jun 10 '19 at 15:35
  • Sorry I just realised that due to editing the part including the 'geom_bar()' was not visible, I think it is clearer now. Thanks everyone, I will try everything you advised me. – Anna Jun 10 '19 at 15:47
  • The answer marked as duplicate is obviously the wrong one and has unfortunately nothing to do with your question. Some people unfortunately jump and close question without much thoughts. However when you use lattice plot in a for loop and nothing happens you should have a look at this one: https://stackoverflow.com/questions/15678261/r-ggplot-does-not-work-if-it-is-inside-a-for-loop-although-it-works-outside-of – Calimo Jun 11 '19 at 15:03
  • @Calimo Yes this is too bad, because I think I found the solution and I wont be able to share it now that the question is closed. Thanks very much for your comment – Anna Jun 12 '19 at 10:50

0 Answers0