2

I have a dataframe df. I need to plot the variables that are factor. For example here it is x,a and b

   df
   x y g  a  b
 1 a 2 1  df df
 2 a 3 2  fg fg
 3 b 4 1  gf gf
 4 b 5 2  fd fd

I have written a code below but I am not able to execute

  as <- names(Filter(is.factor, df))
  for(i in 1:length(as))
  {
  ssa <- ggplot(data=df,aes_string(x=as[i],fill=as[i]))+geom_bar(stat = 
  "count")
  } 
Rfer R
  • 69
  • 5
  • Hi User. I did see this but this is not solving my problem. I need to have 3 plots at a time for x, a and b – Rfer R Aug 18 '19 at 10:05
  • I have edited the code for you. the issues ssa is returning the last plot. What about the first 2 plots – Rfer R Aug 18 '19 at 10:11
  • Have a look at dput function... – Christoph Aug 18 '19 at 10:15
  • Does that help https://stackoverflow.com/q/20953594/5784831? – Christoph Aug 18 '19 at 10:17
  • Hi Christoph, Nope. I am aware of multiple plots but I need that in for loop. Can you please run my code and see. I am getting the final plot. I am not able to store first 2 plots in ssa – Rfer R Aug 18 '19 at 10:19
  • 1
    @RferR, it is quite unclear what you are asking. I'd suggest editing your question specifying whether you need the plots to be `side by side` as in `par(mfrow = c( r, c )` if one used base R (here one could use the `gridExtra` package) or if you need the visuals to be in the same plot as in stacked bar chars or side by side bar charts? Currently the question is ambigous as such answers and comments will be mislead. – Oliver Aug 18 '19 at 10:31
  • Feel free to draw the end figure by hand to illustrate what you're after. Right now it's not very clear to me, sorry. – Roman Luštrik Aug 18 '19 at 14:57

1 Answers1

0

An immediate fix to your code is to add index to store objects.

library(ggplot2)

ssa <- vector("list", length(as))

for(i in 1:length(as)) {
   ssa[[i]] <- ggplot(data=df,aes_string(x=as[i],fill=as[i])) + 
               geom_bar(stat = "count")
} 

Now you have all three objects in ssa[[1]], ssa[[2]] and ssa[[3]] respectively.

However, there are better ways to do the same thing. You can use lapply

plot_obj <- lapply(df[as], function(x) 
            ggplot(data= df, aes(x, fill = x)) + geom_bar(stat = "count"))

Or maybe use facets after gathering the data to long format.

tidyr::gather(df[as]) %>%
   ggplot() + aes(value, fill = value) + geom_bar(stat = "count") + 
   facet_wrap(.~key)

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Perfect thanks. Can we arrange the plots in the layout? Say, I need the third plot in the second row – Rfer R Aug 18 '19 at 14:04
  • @RferR You can use `factor` to adjust order. Do `tidyr::gather(df[as]) %>% mutate(key = factor(key, levels = c("a", "x", "b"))) %>% ggplot() + aes(value, fill = value) + geom_bar(stat = "count") + facet_wrap(.~key)` – Ronak Shah Aug 18 '19 at 14:10
  • Nope. The thing is I did not understand the above code. That is the reason i have asked a question here https://stackoverflow.com/questions/57545652/is-there-a-r-function-to-adjust-layout-under-for-loop – Rfer R Aug 18 '19 at 15:01
  • Can you try to modify my code there? – Rfer R Aug 18 '19 at 15:01