2

My data set contains several categorical variables that I would like visualise to see the distribution.

For example, if I wanted to visualise the 4 variables (manufacturer, trans, fl, class) in the mpg data set in ggplot2, I have to write 4 lines of code:

ggplot(mpg, aes(manufacturer)) + geom_bar() + coord_flip()
ggplot(mpg, aes(trans)) + geom_bar() + coord_flip()
ggplot(mpg, aes(fl)) + geom_bar() + coord_flip()
ggplot(mpg, aes(class)) + geom_bar() + coord_flip()

Resulting barplot:

enter image description here

How can I write a code to do this more efficiently? loop? apply function? I would like to see each chart one at a time, if possible.

Pramod Gharu
  • 1,105
  • 3
  • 9
  • 18
rishi
  • 23
  • 1
  • 4

1 Answers1

3

Your idea to use lapply is one solution.

This requires aes_string to be used instead aes.

Single plots

This creates single plots per column (name) you supply as first argument to lapply:

lapply(c("manufacturer", "trans", "fl", "class"),
  function(col) {
    ggplot(mpg, aes_string(col)) + geom_bar() + coord_flip()
  })

Combined plots

If you require all plots on one plotting area, you can use miscset::ggplotGrid:

library(miscset) # install from CRAN if required
ggplotGrid(ncol = 2,
  lapply(c("manufacturer", "trans", "fl", "class"),
    function(col) {
        ggplot(mpg, aes_string(col)) + geom_bar() + coord_flip()
    }))

The result looks like:

enter image description here

setempler
  • 1,681
  • 12
  • 20
  • That's fantastic! Thanks for the quick response @setempler. Is there a way to order the bars by count? – rishi Aug 31 '16 at 07:17
  • @rishi Yes, described here: http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph - answer of Alex Brown – setempler Aug 31 '16 at 07:20
  • @setempler Can we label the values for each graph and applying the colour for each bar? thankyou. – dondapati Nov 24 '17 at 13:25